Recently I faced an issue on a custom page developed on SharePoint. In this page I am showing a grid and there is a button for exporting the grid data to an excel sheet. The grid also have sorting and pagination functionalities.
After deploying the page, I learnt that, after click on 'Export to excel' link the rest of the control stops functioning. This means the data sorting and pagination links are inactive. Following codes are used for exporting:
protected void lnkExportTop_Click(object sender, EventArgs e)
{
try
{
DataTable dtProjects = (DataTable)ViewState["Projects"];
dtProjects.DefaultView.Sort = "MPP_Name Desc";
gvMerge.DataSource = dtProjects.DefaultView;
gvMerge.AllowPaging = false;
gvMerge.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", "attachment; filename=ProjectDashboard.xls" );
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a table to contain the grid
Table table = new Table();
// include the gridline settings
table.GridLines = GridLines.Both;
// add the header row to the table
if (gvMerge.HeaderRow != null)
{
PrepareControlForExport(gvMerge.HeaderRow);
table.Rows.Add(gvMerge.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gvMerge.Rows)
{
table.Rows.Add(row);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
catch (Exception ex)
{
HandleError("Export to excel ", ex.ToString());
}
}
private void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is Image)
{
control.Controls.Remove(current);
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
Initially I thought that there is some problems with my code. But after several debugging cycle I concluded that my code is working fine.
After some researching on the net, I found that SharePoint security model basically causing the problem. Some client scripts need to call on the OnClilentClick method of the export link button.
< asp:LinkButton ID="lnkExportBottom" runat="server" Text="Export to excel" OnClientClick="_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;"
OnClick="lnkExportTop_Click" / >
Thursday, October 21, 2010
Hiding left blank space of Application.master in SharePoint
While developing on SharePoint once I required to use the built-in Application.master page. This master page is attached to my custom aspx page. After deployed the aspx page I noticed that, there is some blank space on the left portion of the page.
I used the following scripts to hide the left unused space and finally the page is able to use more screen space.
< script language="javascript" type="text/javascript" >
document.getElementById('LeftNavigationAreaCell').style.display = 'none';
document.getElementById('TitleAreaImageCell').style.display = 'none';
document.getElementById('onetidMainBodyPadding').style.display = 'none';
< /script >
After adding this script the finally looks like this:
Subscribe to:
Posts (Atom)