Thursday, June 7, 2012

ASP.Net Tree view check uncheck problem.

Recently I faced issue to check UN-check tree view nodes of a multilevel tree bound with SharePoint document library structure. I searched google but could not found any easy solution so decided to resolve it simply on my own. I wrote "TreeNodeCheckChanged" event. From it I called a recursive function.

  private void CheckUnCheck(TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes != null && e.Node.ChildNodes.Count > 0)
            {
                foreach (TreeNode childNode in e.Node.ChildNodes)
                {
                    if (e.Node.Checked)
                    {
                        if (!childNode.Checked)
                            childNode.Checked = true;                       
                    }
                    else
                    {
                        if (childNode.Checked)
                            childNode.Checked = false;
                    }
                    if (childNode.ChildNodes != null && childNode.ChildNodes.Count > 0)
                    {
                        CheckUnCheck(new TreeNodeEventArgs(childNode));
                    }
                }
            }
        }
this is not done yet. This function wont fire (I don't know why...) so I used a simple trick in Client side called a javascript function like below.

function CheckChangeEvent(event) {
        var obj = null;
        if ((navigator.appVersion.indexOf("MSIE 7.0") > 0) || (navigator.appVersion.indexOf("MSIE 8.0") > 0)) {
            obj = window.event.srcElement;
        }
        else {
            obj = event.target;           
        }
        if (obj == null) {
            alert('Object not found!');
            return false;
        }           
        if (obj != null) {
            if (obj.tagName == "INPUT" && obj.type == "checkbox") {
                __doPostBack(obj.id, '');
            }
            return true;
        }
        else {
            return false;
        }
    }

It do as post back on each click but does the trick... Hope this will help someone.

Wednesday, March 14, 2012

Export data table to Excel

To Export Data table to excel

following code can be useful


SPContext context = SPContext.Current;
System.Web.HttpResponse currentResponse = this.Response;
foreach (DataColumn column in table.Columns)
{
currentResponse.Write(column.ColumnName + "\t");
}
currentResponse.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++) { currentResponse.Write(row[i].ToString().Replace(";", string.Empty) + "\t"); } currentResponse.Write(Environment.NewLine); } currentResponse.ContentType = "text/xls"; currentResponse.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".xls"); currentResponse.End(); currentResponse.Close();
HOPE THIS HELPS SOMEONE

Export Nested SPGridViews to Excel

I spend entire day to resolve bugs for rendering HtmlTextWriter to excel in a visual web part, for each solution you need to modify either web.config or @page directive or override VerifyRenderingInServerForm method on page... It started to spinning my head, then I thought to do something by my own...

1) On ClientClick of link/image/button find GirdView's innerHTML
2) Assign that innerHTML to a hidden field
Add following code to your serverside event

System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.Clear();
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("YOURFILE", System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.Write('>meta http-equiv=Content-Type content=text/html;charset=UTF-8<');
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.Write(">TABLE Gridline=\"Both\"<");
htmlWriter.Write(hdnGridInnerHTML.Value.ToString());
htmlWriter.Write(">/TABLE<");
curContext.Response.Write(strWriter.ToString());
curContext.Response.Flush();
curContext.Response.End();

THERE is one Problem still I am facing... If any body can help.
If you open this document it won't show you correctly. BUT if you Save it locally and then open it opens correctly...

HOPE THIS MIGHT HELP SOMEONE

Limit Sharepoint Site Search with Current SPSite Object

Previously I tried a lot to limit my site search within the local site but could not succeeded.

If you want to create a simple site search utility in your web part page.

I found useful link at HERE, Thanks to Josh...

I created a visual web part and added following code


<script language="javascript" type="text/javascript">
function RedirectToSearchResultsPage() {
var searchVal = document.getElementById('SearchTerm').value;
window.location = "searchresults.aspx?k=" + searchVal + "&cs=This Site" + "&u=<%= SPContext.Current.Site.Url %>"; } </script> <div class="listDiv"> <table width="100%"> <tr> <td> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" class="listTitle-tms"> Search Site </td> </tr> <tr> <td> <input id="SearchTerm" style="width: 70%" /> <input type="button" onclick="RedirectToSearchResultsPage()" value="Search" /> </td> </tr> </table> </td> </tr> </table> </div>


(REMOVE br Tags)
Hope you find it useful.

Monday, October 31, 2011

Custom search and result web part page with quick launch Sharepoint 2010.

I recently faced an issue to create custom site search in a web part page. I solved it using following steps.

NOTE: I did this with SPS2010 Foundation.
  • Create a web part with following code (Remove br tags from code).



  • Then put this web part on a search page.
  • Then create new web part page and open it with SPD.
  • Find asp:content contentplaceholderid="PlaceHolderMain" runat="server" tag.
  • Just below it put this line.(Without curly braces.)
  • Just below your web part zone complete tag in main content place holder put following code.

  • Save file it will prompt for reload click Yes.
  • Check with your custom search and search result pages.

Wednesday, July 1, 2009

Custom Login page CSS and master page on WSS 3.0 Forms Authentication.

  1. Go to "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS" on your WSS Server.
  2. Copy login.aspx and simple.master files there and rename them (I gave MyComp_Login.aspx and MyComp_simple.master).
  3. Copy \12\TEMPLATE\THEMES Folder to Layout folder and change its name suitable for your application.
  4. Open your custom master file (For my application "MyComp_simple.master") with Visual Studio (I opened with VS 2008) add HTML link tag to your custom.css(link rel="stylesheet" type="text/css" href="/_layouts/My_Themes/MyCompTheme/theme.css") just below <Sharepoint:CssLink tag and save changes.
  5. Open MyComp_Login.aspx and replace value of MasterPageFile = "/_layouts/MyComp_simple.master".
  6. Now open your web.config from "c:\Inetpub\wwwroot\wss\VirtualDirectories\PORT_Number"
  7. Find "/_layouts/login.aspx"

  8. Replace with "/_layouts/MyComp_login.aspx"

    AND Save changes.
  9. Resest IIS, Clear Private data and History from browser and open your application from new instance of browser.
  10. Hope you will make it as me...;)

--NH