Export GridView to Excel in web part
Locked
-
Wednesday, December 10, 2008 11:27 AM
Hello
I was trying to export grid view to excel, in pure asp.net it works fine. But in Sharepoint web part after I click Export to Excel link button, other controls stop working and work only after manual page refresh. How to make it work in sharepoint?
//export to excel click handler protected void BtnExportGrid_Click(object sender, EventArgs args) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader( "content-disposition", string.Format("attachment; filename={0}", "Customers.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(); // add each of the data rows to the table foreach (GridViewRow row in gv.Rows) { GridViewExportUtil.PrepareControlForExport(row); 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(); } } }
Answers
-
Wednesday, December 10, 2008 12:03 PM
Problem solved Just add following script on page<script type='text/javascript'>
_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;
</script>
Thanks to http://www.marten-online.com/sharepoint/ajax-second-postback-not-working-in-sharepoint-in-updatepanel.html- Marked As Answer by falcon81 Wednesday, December 10, 2008 12:03 PM

