简体   繁体   中英

RegisterForEventValidation can only be called during render() and Unable to evaluate expression because the code is optimized

I want to export the gridview to excel. For that i have done the following coding

    Response.Clear();
    Response.ContentType = "application/ms-excel";
    Response.Charset = "";
    Page.EnableViewState = false;
    Response.AddHeader("Content-Disposition", "inline;filename=report.xls");
    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    GridView1.RenderControl(hw);
    Response.Write(tw.ToString());
    Response.End();



public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}

First i got the following error Unable to evaluate expression because the code is optimized or a native frame is on top of call stack

so that in my page i have added

 EnableEventValidation="true"

No i am getting the following error RegisterForEventValidation can only be called during render()

How to solve the issue?

I have solved the issue. I have created a new page and put the coding. It workibg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM