asp.netgridviewupdatepanelimagebuttonasplinkbutton

Using LinkButton inside GridView and UpdatePanel getting 0x800a139e error


I use a Gridview inside an UpdatePanel. In that Gridview I use a LinkButton (I have the same problem with an imageButton) for the CommandArgument and try to download a pdf file when the user clicks.

It was tricky to get the Button running in that UpdatePanel but all seems fine. The user clicks the Button and I get the correct ID with the CommandArgument. With this ID I try to Response the pdf file but getting the error:

0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

The pdf file is on the server, with an button outside the UpdatePanel I get the right pdf file. So what to I have to change to get this inside the UpdatePanel running? Thanks

Gridview:

 <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:LinkButton ID="Fehlermeldung" runat="server" CommandArgument='<%#Eval("ID")%>' OnClick="Fehlermeldung_Click1">
                     <asp:Image ID="Fehler" ImageUrl="~/images/fehlermeldung.png" ToolTip="Fehlermeldung anzeigen" runat="server" />
                    </asp:LinkButton>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>

Code behind:

protected void Fehlermeldung_Click1(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;

            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;Filename=Fehlermeldung_" + id + ".pdf");
            Response.TransmitFile(Page.MapPath(@"App_Data\Fehlermeldung_" + id + ".pdf"));
            Response.End();
    }

Solution

  • The problem is your Fehlermeldung link button is contained inside TemplateField, hence you cannot directly reference it like normal LinkButton does.

    You should handle RowDataBound event of the GridView to find the link button by its control name and put button postback triggers programmatically there using RegisterPostBackControl() (GridViewName is your grid view ID):

    ASPX Markup

    <asp:GridView ID="GridViewName" OnRowDataBound="GridViewName_RowDataBound" runat="server" ...>
        <%-- gridview contents --%>
    </asp:GridView>
    

    Code behind

    protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // find link button first
            LinkButton btn = e.Row.FindControl("Fehlermeldung") as LinkButton;
    
            // use postback trigger assignment
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn);
        }
    }
    

    Reference:

    Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed