I’m trying to export data from DataGridView into Excel sheet, so I used the following code, but at bolded and underlined line I’m getting an exception that says “{"RegisterForEventValidation can only be called during Render();"}”
How should I solve this?
private void ExportGridView()
{
try
{
string attachment = "attachment; filename = Report.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView1);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch (Exception e1)
{
Response.Write(e1);
}
}
The solution for this is by setting the “EnableEventValidation” as False
பதிலளிநீக்குIt worked for me. I thank everyone for their effort in letting me know the solution.