【问题标题】:How to download a pdf from a panel in an update panel如何从更新面板中的面板下载 pdf
【发布时间】:2015-07-22 22:30:10
【问题描述】:

我有一个使用母版页的网站。

我的一个内容页面基本上是一个大的 UpdatePanel。 UpdatePanel 内部是一个常规面板。常规面板内部是一个 Gridview。 Gridview 内部是一个链接按钮,它指向存储在我的数据库中的 pdf。

当我单击链接按钮检索 pdf 时,什么也没有发生。

我在另一个没有 UpdatePanel 的页面上工作。

我已经尝试从 Linkbutton 触发“外部”按钮并将该按钮注册为 PostBack 事件。当我单击链接按钮时页面回发,但 pdf 没有发送给用户。

这里是一些示例代码:

<asp:UpdatePanel ID="UpdatePanelClaims" runat="server">
<ContentTemplate>

<asp:Panel ID="upClaimAttachment" runat="server" Visible="false" >

        <table id="gridClaimAttachmentTable" runat="server" class="table" >
            <tr>
                <td >
                    <asp:GridView ID="grdClaimAttachment" runat="server" AllowPaging="True" AllowSorting="True" 
                        AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-condensed table-hover" EmptyDataText="No Attachments for this Claim."
                        EnableTheming="False" onpageindexchanging="grdClaimAttachment_PageIndexChanging"   PageSize="15" OnRowCommand="grdClaimAttachment_RowCommand"
                        OnRowDataBound="grdClaimAttachment_RowDataBound" >
                        <PagerStyle CssClass="bs-pagination" />
                        <AlternatingRowStyle CssClass="alternateColor" />
                        <RowStyle CssClass="rowsStyle" />
                        <Columns>
                            <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="hideColumn" HeaderStyle-CssClass="hideColumn" >
                                <HeaderStyle HorizontalAlign="Left" />
                                <ItemStyle HorizontalAlign="Right" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="File Name">
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnViewAttachment" Text='<%#Eval("FileName") %>' CommandName="ViewAttachment"
                                        CommandArgument="<%# Container.DataItemIndex %>" runat="server"></asp:LinkButton></ItemTemplate>
                            </asp:TemplateField>

                            <asp:ButtonField ButtonType="Button" CommandName="btnDelete" Text="Delete">
                                <ControlStyle CssClass="btn btn-info btn-xs " />
                            </asp:ButtonField>
                        </Columns>
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>
                </td>
            </tr>
            <tr >
                <td>
                    <div class="container">
                        <div class="form-group form-group-sm form-groupNoSpace">
                            <div class="row">
                                <div class=" col-xs-4 col-xs-offset-4 text-right">
                                    <asp:Button ID="btnClaimAttachmentAdd" runat="server" CssClass="btn btn-primary btn-sm btn-block" Text="Add Attachment" OnClick="btnClaimAttachmentAdd_Click"/>
                                </div>
                            </div>
                        </div>
                        </div>

                </td>
            </tr>
        </table>

</asp:Panel> <%-- Attachment Update Panel --%>

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

</ContentTemplate>
  <Triggers>
      <asp:PostBackTrigger ControlID="btnClickMe" />
  </Triggers>
</asp:UpdatePanel> <%-- UpdatePanelClaims --%>

在我后面的代码中:

    protected void btnClickMe_Click(object sender, EventArgs e, ClaimAttachment objAttachment)
    {
        ViewAttachment(objAttachment);
    }

    private void ViewAttachment(ClaimAttachment objAttachment)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.AppendHeader("content-disposition", "attachment;filename=" + objAttachment.FileName);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(objAttachment.Attachment);
        Response.Flush();
        Response.End();
    }

更新:忘记了一些关键代码!

    protected void grdClaimAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (index >= grdClaimAttachment.Rows.Count)
                return;

            int IDkey = Convert.ToInt32(grdClaimAttachment.Rows[index].Cells[0].Text);
            ClaimAttachment objClaimAttachment = ClaimAttachment.RetrieveById((string)Session["Username"], IDkey);

            if (e.CommandName == "btnDelete")
            {
                ltlDeleteID.Text = IDkey.ToString();
                ltlRecordType.Text = "attachment";
                confirmDialog(string.Format("DELETE Attachment: {0} ?", objClaimAttachment.FileName));
            }
            else if (e.CommandName == "ViewAttachment")
            {
                //btnClickMe.CommandArgument = IDkey.ToString();
                //btnClickMe_Click(sender, e);
                btnClickMe.Click += new EventHandler((s1, e1) => btnClickMe_Click(s1, e1, objClaimAttachment));
                btnClickMe_Click(sender, e, objClaimAttachment);
            }
        }
        catch (BLException be)
        {
            errDialog(be.Message);
        }
    }

网格中的Linkbutton其实是在调用外部按钮的Click事件来进行pdf下载...

我错过了什么?就像我说的那样,如果我删除所有 UpdatePanels 但我需要它们用于其他事情...

谢谢!

【问题讨论】:

    标签: c# asp.net gridview updatepanel postback


    【解决方案1】:

    PostBackTrigger 类是解决方案的关键,因为它可用于触发下载响应正常工作所需的完整页面重新加载。部分回发根本无法下载。

    但是,由于应该触发回发的按钮在您的网格内,因此在页面标记中使用单个 PostBackTrigger 是不够的,您需要为每个按钮/行设置一个特定的触发器

    使用类似的东西(从您的 Page_Load 调用)

    private void RegisterPostBackControls()
    {
        foreach (GridViewRow row in grdClaimAttachment.Rows)
        {
            LinkButton button = row.FindControl("btnViewAttachment") as LinkButton;
            ScriptManager.GetCurrent(this).RegisterPostBackControl(button);
        } 
    }
    

    【讨论】:

    • 我在原始帖子中添加了更多细节,展示了我是如何注册外部按钮的点击事件的。我还尝试使用您建议的方法 - 删除外部按钮,在 gridview 上注册 View Attachment 按钮并直接从附件网格的 RowCommand 调用 ViewAttachment 过程(检索 pdf),但仍然无法正常工作......谢谢!
    • 感谢您的建议...但是我发现解决此问题的唯一方法是基本上删除外部更新面板并使用标准面板。这是我第三次遇到这个问题,我还没有以任何其他方式解决它。真奇怪!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多