Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed - Link Button within Gridview

十年热恋 提交于 2019-12-11 18:20:36

问题


I am currently working on VS 2010. I have a gridview on my content page. I am trying to download a file from the server.

The file downloading functionality happens on the click of a link button which is placed within the gridview for each individual record. The gridview is placed within an update panel and the Script Manager is placed on the master page. Also, i have made use of bootstrap on my page.

On the click of the link button withing the gridview the following error is displayed

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

I have searched the internet for solutions but could not find any fix. I came across links where the buttons where placed outside the grid and a similar issue was faced.But, did not find it helpful.

I have tried with using a "PostBackTrigger". But that does not fix the issue. I have referred the below link but it does not provide a solution to the issue suggested above

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

I have also referred other links but could not find a fix to the problem. Also removing the Update Panel is not an option.

I am placing the related design and code of my page.

Design -

<div class="col-xs-12 form-group">
                <div class="table-responsive">
                    <asp:UpdatePanel ID="updPnlErrorDownload" runat="server" UpdateMode="Conditional">
                    <%--<Triggers>
                        <asp:AsyncPostBackTrigger  ControlID="lnkLogFiles"/>
                    </Triggers>--%>
                        <ContentTemplate>
                            <asp:GridView ID="gvLogFilesDownload" runat="server"
                                AutoGenerateColumns="false" AllowPaging="true" 
                                EmptyDataText="No Data Found" Width="100%" 
                                CssClass="table table-striped table-bordered table-hover" PageSize="10" 
                                onpageindexchanged="gvLogFilesDownload_PageIndexChanged" 
                                onrowcommand="gvLogFilesDownload_RowCommand" 
                                onrowcreated="gvLogFilesDownload_RowCreated">
                                <Columns>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Log Files">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkLogFiles" runat="server" Text='<%#Eval("Log Files") %>' Font-Underline="true" CommandName="Download" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"  OnClick="lnkLogFiles_Click"/>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    No Record Available</EmptyDataTemplate>
                            </asp:GridView>
                        </ContentTemplate>
                        <Triggers>
                        <%--<asp:PostBackTrigger ControlID="lnkLogFiles" />--%>
                       <%-- <asp:PostBackTrigger ControlID="gvLogFilesDownload$lnkLogFiles" />--%>
                    </Triggers>
                    </asp:UpdatePanel>
                </div>
            </div>

Code -

protected void lnkLogFiles_Click(object sender, EventArgs e)
{
    LinkButton lnkBtnDownload = sender as LinkButton;
    string file = lnkBtnDownload.Text;
    //string sPath1 = Server.MapPath(file);
    string sPath = Server.MapPath("~/ErrorLog/" + file);
    //Response.ContentType = "APPLICATION/OCTET-STREAM";
    Response.ContentType = "APPLICATION/pdf";
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(sPath));
    Response.TransmitFile(sPath);
    Response.End();
}

Any advice / suggestions is greatly appreciated. Thanks in advance.


回答1:


The problem is that those controls are in a template so you cannot reference them directly. Use the RowDataBound event and assign the buttons a trigger programatically.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow and not the first row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the button with findcontrol
        LinkButton lb = e.Row.FindControl("lnkLogFiles") as LinkButton;

        //assign the button as a postback trigger
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(lb);
    }
}


来源:https://stackoverflow.com/questions/48340544/sys-webforms-pagerequestmanagerparsererrorexception-the-message-received-from-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!