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

}

这样问题解决了,以下是转载一篇CSDN同仁的文章,谢谢

错误消息:

Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。

 

发生状况:

页面使用了ajax的UpdatePanels,在使用excel导出时,使用了Response.WriteFile这样的方法,所以出现上述错误。

 

解决办法:

1、将使用了Response.Write的控件放到啊UpdatePanels外面,或者将UpdatePanels删除。当然大多数情况下,我们是不可以删除UpdatePanels的。

 

2、如果控件的名称是唯一的,可以使用在UpdatePanels里增加PostBackTrigger ,并制定controlid为你的控件:

  <Triggers><asp:PostBackTrigger ControlID="btnExport" /></Triggers>

 

3、如果你的控件是动态的,比如在gridview每行中都有的按钮,可以使用ScriptManager.RegisterPostBackControl()来注册你的控件

 protected void gvGirdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //不为表头
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //含有ajax的updatepanel的页面必须将想要使用Response.Write的控件注册PostBackControl
                //本业务使用了excel导出,所以必须绑定

               //我的业务用到了模板页,ScriptManager 定义在模板页上,所以使用this.Parent.Parent到上级去找ScriptManager 

               //实际可根据自己程序情况查找
                ScriptManager smManage = (ScriptManager)this.Parent.Parent.FindControl("smManage ");
                if (e.Row.FindControl("btExport") != null)
                {
                    smManage .RegisterPostBackControl(e.Row.FindControl("btExport"));
                }
            }
        }

转载至http://blog.csdn.net/liyun919/article/details/5613510

posted @ 2013-04-17 10:42  AndyDu  阅读(719)  评论(0编辑  收藏  举报