To export your grid data
public static void ExportToExcel(string strFileName, GridView dg)
{
try
{
HttpContext.Current.Response.ClearContent();
int row = dg.Rows.Count;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + strFileName + System.DateTime.Now.Hour + "_" + System.DateTime.Now.Minute + ".xls");
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
catch(Exception ex)
{
}
}
and add these lines as it is to verify rendering of control
public override void VerifyRenderingInServerForm(Control control)
{
}
Now In your .aspx file in <%@page .... %> add atributes
AutoEventWireup="true" EnableEventValidation="false"
this works for me hope will help you too .. happy coding :-)