Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。

来源:互联网 发布:知向濂洛之学 编辑:程序博客网 时间:2024/05/17 18:03

Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。

今天在GridView的模板列中要实现一个下载附件的功能,下载的代码如下:

      /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="page">当前页对象</param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileContext">文件字节流</param>
        public static void DownLoadFile(System.Web.UI.Page page, string fileName, byte[] fileContext)
        {
            page.Response.Buffer = true;
            page.Response.Clear();
            page.Response.ContentType = "application/octet-stream";
            page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
            page.Response.BinaryWrite(fileContext);
            page.Response.Flush();
            page.Response.End();
        }

结果一运行,问题来了 Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。错误!

在网上搜罗了半天终于找到答案,一看页面用了   <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false" CssClass="grid"
                        OnRowDataBound="gvList_RowDataBound" DataKeyNames="ID" AllowSorting="true" OnSorting="gvList_Sorting"
                        RowStyle-HorizontalAlign="Left" OnRowCommand="gvList_RowCommand">
                        <Columns>
                            <asp:CheckBoxField ItemStyle-Width="40" ItemStyle-HorizontalAlign="Center" />
                            <asp:BoundField HeaderText="序号">
                                <ItemStyle HorizontalAlign="Center" Width="50"></ItemStyle>
                            </asp:BoundField>
                                           <asp:BoundField HeaderText="标题" DataField="Name" SortExpression="Name"></asp:BoundField>
                                                      <asp:BoundField HeaderText="文件状态" DataField="FileStatus" SortExpression="FileStatus">
                                <ItemStyle Width="60" HorizontalAlign="Center"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="版本" DataField="VersionNo" SortExpression="VersionNo">
                                <ItemStyle Width="60" HorizontalAlign="Center"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="注销原因" DataField="CancelReason">
                                <ItemStyle Width="150" HorizontalAlign="Center"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="注销时间" DataField="CancelDate"  DataFormatString="{0:yyyy-MM-dd}">
                                <ItemStyle Width="80" HorizontalAlign="Center"></ItemStyle>
                            </asp:BoundField>
                             <asp:TemplateField>
                                <ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
                                <HeaderTemplate>
                                    注销附件
                                </HeaderTemplate>
                                <ItemTemplate>
                                  <asp:Button ID="btnDownload" runat="server" CommandName="DownLoad" CausesValidation="False" CssClass="btnGrid"
                                        Text="下载"></asp:Button>
                               </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
                                <HeaderTemplate>
                                    操作</HeaderTemplate>
                                <ItemTemplate>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <div class="tip">
                                暂无记录!</div>
                        </EmptyDataTemplate>
                    </asp:GridView>

 </asp:UpdatePanel>

发现在gvList_RowDataBound的时候未对下载按钮动态注册,导致了错误的发生,该事件中添加如下代码:

if (e.Row.RowType == DataControlRowType.DataRow)
{

    //注册下载按钮的事件
                        ScriptManager sm = (ScriptManager)this.Page.FindControl("ScriptManager1");
                        if (e.Row.FindControl("btnDownload") != null)
                        {
                            sm.RegisterPostBackControl(e.Row.FindControl("btnDownload"));
                        }

}

原创粉丝点击