Click here to Skip to main content
15,915,658 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to export data from gridview to execl but

there is execption "RegisterForEventValidation can only be called during Render();"
how to slove this?????
C#
protected void Button3_Click(object sender, EventArgs e)
{   
    ExportGridView2Excel(GridView1, "test.xls");
}

private void ExportGridView2Excel(GridView gridViewControl, string excelFileName)
{
    if (string.IsNullOrEmpty(excelFileName))
        throw new Exception("excelFileName is missing");
    if (!excelFileName.EndsWith(".xls"))
        excelFileName += ".xls";
    gridViewControl.AllowPaging = false;
    gridViewControl.AllowSorting = false;
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader(
        "content-disposition",
        "attachment;filename=" + excelFileName);
    gridViewControl.RenderControl(hw); // execption in this line
    Response.Write(tw.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
    return;
}
Posted
Updated 25-Apr-13 21:29pm
v3

Add
C#
EnableEventValidation="false"
at top of your aspx page in page directive.
 
Share this answer
 
v3
Just add
C#
EnableEventValidation="false"
to your page directive.
And if you searched CP you could have ended in this Q&A : RegisterForEventValidation can only be called during Render();[^]
Good luck,
OI
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900