Search code examples
c#asp.netgridviewupdatepanelscriptmanager

Error in UpdatePanel: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack


I use a UpdatePanel with a Gridview in it. A Timer is checking some things in the Gridview. In the Gridview I generate a column for viewing an upload:

<asp:TemplateField HeaderText="Zeichnung" ItemStyle-HorizontalAlign="Center">

Code behind:

 ImageButton ibtn = new ImageButton();
 ibtn.CommandArgument = Upload;
 ibtn.Click += btn_clicked;
 ibtn.ImageUrl = "~/images/download.png";
 ibtn.ToolTip = "Zeichnung öffnen";
 gvr.Cells[20].Controls.Add(ibtn);

Clicking the button start the download:

Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;Filename=" + Upload);
Response.TransmitFile(Page.MapPath("App_data/OPL/Upload/" + Upload));
Response.End();

All worked fine until I put the gridview into the UpdatePanel. Now it throws the error:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I tried to replace the Response.End() with HttpContext.Current.ApplicationInstance.CompleteRequest(); but nothing changed.

As written here (question) its a problem of the postback. The solution seems to be

protected void Page_Load(object sender, EventArgs e) {
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExcelExport);
//Further code goes here....
}

But I cannot add this code because the button is generated and not given in the gridview. I tried to add scriptManager.RegisterPostBackControl(this.btn_Upload); but he is not finding the button.

How can I solve this? Thanks


Solution

  • A possible solution is to register the gridview (as a whole) as the postback control.ScriptManager.RegisterPostBackControl(this.gvr);