我正在尝试从 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 无法识别我的链接按钮...
任何帮助,将不胜感激!让我知道是否需要更多代码和详细信息。提前致谢!