You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

回发后下载出现PageRequestManagerParserErrorException异常求助

Fixing PageRequestManagerParserErrorException for Downloads in UpdatePanel

Ah, I’ve run into this exact issue before! The problem happens because the UpdatePanel uses AJAX partial postbacks, which expect a specific JSON/XML response from the server. When you try to send a file download response (binary or non-standard content type), the AJAX framework can’t parse it—hence the PageRequestManagerParserErrorException.

Here are two reliable solutions to fix this:

Solution 1: Register the LinkButton as a Full Postback Control

Since your LinkButton is inside a Repeater, we can’t declaratively add it as a trigger to the UpdatePanel. Instead, we’ll programmatically register it to trigger a full postback using the ScriptManager.

Step 1: Handle the Repeater’s ItemDataBound Event

In your code-behind, add this event handler for the rptInfo Repeater:

protected void rptInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // Check if we're dealing with a data item
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // Find the LinkButton in the current item
        LinkButton lnkSaveInfo = e.Item.FindControl("lnkSaveInfo") as LinkButton;
        if (lnkSaveInfo != null)
        {
            // Register the button to trigger a full postback
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(lnkSaveInfo);
        }
    }
}

Don’t forget to wire up the event in your ascx:

<asp:Repeater ID="rptInfo" runat="server" OnItemDataBound="rptInfo_ItemDataBound">

Step 2: Properly Send the Download Response

In your LinkButton’s command handler (since you’re using CommandName="saveInfo"), make sure to clear the response and end it after sending the file:

protected void lnkSaveInfo_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "saveInfo")
    {
        // Replace with your logic to get the file data and name
        byte[] fileContent = GetFileContent(); // Your method to fetch the file
        string fileName = "your-document.pdf";

        // Clear any existing response content
        Response.Clear();
        Response.ContentType = "application/pdf"; // Adjust based on your file type
        Response.AddHeader("Content-Disposition", $"attachment; filename=\"{fileName}\"");
        Response.BinaryWrite(fileContent);
        Response.Flush();
        Response.End(); // Critical to stop further page processing
    }
}

Wire up the Command event in the Repeater’s ItemTemplate:

<asp:LinkButton runat="server" ID="lnkSaveInfo" CommandName="saveInfo" OnCommand="lnkSaveInfo_Command" />

Solution 2: Use a Hidden Button Outside the UpdatePanel

If you prefer not to modify the postback behavior of the LinkButton directly, you can use a hidden button outside the UpdatePanel to trigger the download via JavaScript.

Step 1: Add a Hidden Button and Hidden Field (if needed)

Add these controls outside your UpdatePanel:

<asp:Button ID="btnHiddenDownload" runat="server" Text="" Style="display: none;" OnClick="btnHiddenDownload_Click" />
<asp:HiddenField ID="hfFileIdentifier" runat="server" /> <!-- To pass file-specific data -->

Step 2: Modify the LinkButton to Trigger JavaScript

Update your Repeater’s LinkButton to call a JS function instead of a direct postback:

<asp:LinkButton runat="server" ID="lnkSaveInfo" 
    OnClientClick="prepareDownload('<%# Eval("FileId") %>'); return false;" />

Step 3: Add the JavaScript Function

Add this script to your ascx:

function prepareDownload(fileId) {
    // Set the file ID in the hidden field so the server knows which file to send
    document.getElementById('<%= hfFileIdentifier.ClientID %>').value = fileId;
    // Trigger the hidden button's click (full postback)
    document.getElementById('<%= btnHiddenDownload.ClientID %>').click();
}

Step 4: Implement the Hidden Button’s Click Handler

In code-behind, handle the hidden button’s click to send the download:

protected void btnHiddenDownload_Click(object sender, EventArgs e)
{
    string fileId = hfFileIdentifier.Value;
    byte[] fileContent = GetFileContentById(fileId); // Your method to fetch the file
    string fileName = $"document-{fileId}.pdf";

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", $"attachment; filename=\"{fileName}\"");
    Response.BinaryWrite(fileContent);
    Response.Flush();
    Response.End();
}

Key Notes

  • Always use Response.Clear() and Response.End() when sending download responses to ensure no extra content is sent back to the AJAX framework.
  • If you’re using other file types, adjust the ContentType accordingly (e.g., text/csv for CSV files, image/png for PNGs).

内容的提问来源于stack exchange,提问作者Ali Soltani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:06:02