Search code examples
c#asp.netwebformsupdatepanelmaster-pages

C# Download button inside of Update Panel in Master Page


I have a web form that has a Master Page, which this Master Page uses a Update Panel.

The problem is, whenever I try to download a file I got this error :

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

This is the code

    Response.ClearContent();
    Response.ClearHeaders();

    Response.AppendHeader("Content-Disposition", 
    string.Format("filename=Relatorio.{0}", "xlsx"));
    Response.ContentType = "application/vnd.ms-excel";
    Response.BinaryWrite(File_Converted_toBytes);
    Response.Flush();
    Response.End();

If I create a new project for example, a web forms one, and just create a blank page, that has a button with a this code inside, everything works well.

Has somebody any idea ?


Solution

  • The button which initiates the download must trigger a full postback. Being inside an UpdatePanel, it's not possible until you do something like this:

    <asp:UpdatePanel runat="server">
        <Triggers>
            <asp:PostBackTrigger ControlID="YourControlID" />
        </Triggers>
        <ContentTemplate>
            .....
        </ContentTemplate>