【问题标题】:ASP.NET: Response.BinaryWrite inside UpdatePanel inside Child User ControlASP.NET:子用户控件内的 UpdatePanel 内的 Response.BinaryWrite
【发布时间】:2013-02-25 19:43:42
【问题描述】:

所以我有以下场景:

--- Page.aspx ---

UpdatePanel
   ListView
      UserControl.ascx

--- UserControl.ascx ---

    ListView
        Button|ID:btnDownloadAttachment

我使用以下方法下载附件:

public void OpenDocument(byte[] AttContent, string fileName, string inExtension)
{
    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();

    Response.ContentType = "application/pdf";

    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + inExtension);
    Response.AddHeader("Content-Length", AttContent.Length.ToString());
    Response.BinaryWrite(AttContent);
}

但由于内容在更新面板中,我收到以下错误:

“Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: 无法解析从服务器接收到的消息。”

【问题讨论】:

  • 您无法通过 AJAX 下载文件。
  • @SLaks 我需要更新面板,反正我可以解决这个问题吗?
  • 没有。您需要回发。

标签: asp.net user-controls updatepanel parent-child binarywriter


【解决方案1】:

如果你运行 fiddler 来查看响应,我猜你会看到你的下载。问题是部分页面呈现。当客户端得到它认为它应该得到一个页面更新,而是得到一个二进制文件。有几个解决方案:

选项#1,完全禁用页面的部分页面渲染(必须在page_init中完成):

protected void Page_Init(object sender, EventArgs e)
{
  ScriptManager mgr = ScriptManager.GetCurrent(this);
  mgr.EnablePartialRendering = false;
}

选项 #2 强制使用启动下载的控件进行回发:

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(BtnExport);

选项 #3 创建回发触发器

<asp:updatepanel id="UpdatePanel1" runat="server">
    <triggers>
        <asp:postbacktrigger ControlID="BtnExport"/>
    </triggers>
</asp:updatepanel> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多