【问题标题】:Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed - Link Button within GridviewSys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器接收到的消息 - Gridview 中的链接按钮
【发布时间】:2018-06-28 15:53:02
【问题描述】:

我目前正在开发 VS 2010。 我的内容页面上有一个gridview。我正在尝试从服务器下载文件。

文件下载功能发生在点击放置在每个单独记录的网格视图中的链接按钮时。 网格视图放置在更新面板中,脚本管理器放置在母版页上。 另外,我在我的页面上使用了引导程序。

单击带有gridview的链接按钮时,会显示以下错误

JavaScript 运行时错误:Sys.WebForms.PageRequestManagerParserErrorException: 无法解析从服务器接收到的消息。

我已经在互联网上搜索了解决方案,但找不到任何解决方法。 我遇到了按钮放置在网格之外的链接,并且遇到了类似的问题。但是,没有发现它有帮助。

我尝试过使用“PostBackTrigger”。但这并不能解决问题。 我已经参考了下面的链接,但它没有为上面建议的问题提供解决方案

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

我还参考了其他链接,但找不到问题的解决方法。 也不能删除更新面板。

我正在放置我的页面的相关设计和代码。

设计 -

<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>

代码-

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();
}

非常感谢任何意见/建议。 提前致谢。

【问题讨论】:

  • 如果您从 UpdatePanel 中向浏览器发送文件,那么您需要确保 lnkLogFiles 是 PostBack 触发器。
  • 嗨 VDWWD,我已经尝试过这样做。但它会引发错误 - 在 UpdatePanel 'updPnlErrorDownload' 中找不到触发器的 ID 为 'lnkLogFiles' 的控件。另外,我尝试如下获取整个路径 - ...当按钮放置在其他容器中但不适用于 gridview 时,此方法有效。

标签: asp.net gridview webforms updatepanel pagerequestmanager


【解决方案1】:

问题在于这些控件位于模板中,因此您无法直接引用它们。使用 RowDataBound 事件并以编程方式为按钮分配触发器。

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);
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-28
    • 2012-09-01
    • 2015-01-17
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多