显示pdf时出错:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息

时间:2013-10-14 06:47:20

标签: c# asp.net pdf dopostback

我正在尝试从SQL中提取pdf文件并将其显示给用户。我一直收到以下错误: Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。

我的项目正在使用asp.net加载一个gridview,其中包含一个包含pdf的数据库记录的链接按钮。

当用户单击链接按钮时,应从SQL中提取pdf并使用Response.BinaryWrite显示。

这是我的aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ReturnedReports.aspx.cs" Inherits="PP.ApKPI.WEB.ReturnedReports" %>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">
    <h1>Reports</h1>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">

    <asp:UpdatePanel ID="ReportsUpdatePanel" runat="server">
        <ContentTemplate>
            <asp:Panel ID="ReportsPanel" runat="server" Height="611px">
                <h2>
                    Reports
                </h2>
                <asp:Label ID="ReportsNotification" runat="server"></asp:Label>
                <br />
                <asp:Label ID="Label1" runat="server" Text="Pharmacy: "></asp:Label>
                <asp:DropDownList ID="PharmaciesDropDownList" runat="server" AutoPostBack="true"
                    OnSelectedIndexChanged="PharmaciesDropDownList_SelectedIndexChanged">
                </asp:DropDownList>
                <br />
                <br />
                <%--<asp:Button ID="NewReport" runat="server" Text="Add Data" OnClick="NewReport_Click" />--%>
                <asp:GridView ID="ListReportsGridView" runat="server" AllowPaging="true" AllowSorting="true"
                    GridLines="None" PageSize="10" CssClass="GridView" AutoGenerateColumns="false"
                    EmptyDataText="No rows for selection." OnRowCommand="ListReportsGridView_RowCommand"
                    OnPageIndexChanging="ListReportsGridView_PageIndexChanging"
                    OnRowDataBound="ListReportsGridView_RowDataBound" 
                    EnablePersistedSelection="True"
                    DataKeyNames="FinDataID">
                    <AlternatingRowStyle CssClass="GridViewAltRow" />
                    <HeaderStyle CssClass="GridViewHeader" />
                    <RowStyle CssClass="GridViewItem" />
                    <PagerStyle CssClass="GridViewPager" />
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" ControlStyle-CssClass="Hidden" ItemStyle-CssClass="Hidden"
                            HeaderStyle-CssClass="Hidden" FooterStyle-CssClass="Hidden" />
                        <asp:BoundField DataField="FinDataID" HeaderText="FinDataID" ControlStyle-CssClass="Hidden" ItemStyle-CssClass="Hidden"
                            HeaderStyle-CssClass="Hidden" FooterStyle-CssClass="Hidden" />
                        <asp:BoundField DataField="PeriodStart" HeaderText="Period Start" DataFormatString="{0:dd/MM/yyyy}" />
                        <asp:BoundField DataField="PeriodEnd" HeaderText="Period End" DataFormatString="{0:dd/MM/yyyy}" />
<%--                        <asp:ButtonField DataTextField="FileName" HeaderText="Report" CommandName="ViewRpt" />--%>
                        <asp:TemplateField><ItemTemplate><asp:LinkButton ID="LinkButtonViewRpt" Text='<%#Eval("FileName") %>' CommandName="ViewRpt" CommandArgument="<%# Container.DataItemIndex %>" runat="server"></asp:LinkButton></ItemTemplate></asp:TemplateField>    
                        <asp:TemplateField Visible="false"><ItemTemplate></ItemTemplate></asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <br />
            </asp:Panel>

    <asp:Panel ID="UploadPanel" runat="server" >
        <h2>Upload Report for
            <asp:Label ID="UPharmacyName" runat="server"></asp:Label>
        </h2>

        <p>
            <table width="360px">
                <tr>
                    <td>Period Start:</td>
                    <td>Period End:</td>
                </tr>
                <tr>
                    <td><asp:Label ID="lblUPeriodStart" runat="server" Width="50px"></asp:Label></td>
                    <td><asp:Label ID="lblUPeriodEnd" runat="server" Width="50px"></asp:Label></td>
                </tr>
            </table>
        </p>
            Select File to Upload:
            <asp:UpdatePanel ID="UploadPanel2" runat="server">
                <ContentTemplate>
                    <%--<asp:FileUpload ID="UploadFile" runat="server" />--%>
                    <asp:FileUpload ID="UploadFile" runat="server" />
                    <p>
                        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" 
                            Text="Upload" />
                        <asp:Button ID="btnCancelUpload" runat="server" OnClick="btnCancelUpload_Click" 
                            Text="Cancel" />
                </ContentTemplate>
<%--                <Triggers>
                    <asp:PostBackTrigger ControlID="btnUpload" />
                </Triggers>--%>
            </asp:UpdatePanel>
    </asp:Panel>

        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="btnClickMe" runat="server" OnClick="btnClickMe_Click" Visible="false" />
    <p></p>
</asp:Content>

这是背后的部分代码:

private void ViewPdf()
{
    //Response.Clear();
    Response.Buffer = true;
    //Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + objFileData.FileName);
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(objFileData.Data);
    Response.Flush();
    Response.End();
}

正如你所看到的,我在UpdatePanel外面尝试了隐形按钮,并且我已经尝试使用PostBackTriggers无济于事。我无法使PostBackTrigger工作,因为我的链接按钮无法从UpdatePanel识别...

任何帮助将不胜感激!如果需要更多代码,请告知我们。提前谢谢!

2 个答案:

答案 0 :(得分:0)

所以我能够解决这个问题(好吧,不是我合作的人)!

删除外部UpdatePanel(ReportsUpdatePanel)解决了这个问题。我不完全理解它是如何解决它的,但显然它与同步和无序发送http头有关。如果有人想发表评论并澄清这一点,请做!

感谢。

答案 1 :(得分:0)

从asp中删除相关grid()的asp:AsyncPostBackTrigger并添加以下代码(根据您的代码)这只是示例

    页面加载期间
  1. 添加以下代码

    ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(this.ListReportsGridView);

  2. 然后使用auto select button = true。如果你不需要按钮,你可以在你的网格行绑定中将显示设置为无。