0
votes

I have a site that uses master pages.

One of my content pages is basically a large UpdatePanel. Inside that UpdatePanel is a regular Panel. Inside the regular Panel is a Gridview. Inside the Gridview is a Linkbutton that points to a pdf stored in my database.

When I click the Linkbutton to retrieve the pdf, nothing happens.

I have this working on another page that has no UpdatePanel.

I have already tried firing an 'external' button from the Linkbutton and registering this button as a PostBack event. The page posts back when I click the Linkbutton but the pdf is not being sent to the user.

Here is some sample code:

<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 --%>

In the code behind I have this:

    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();
    }

UPDATE: Forgot some critical code!

    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);
        }
    }

The Linkbutton in the grid is actually calling the Click event of an external button to perform the pdf download...

What am I missing?? Like I said, this works if I remove all UpdatePanels but I need them for other things...

thx!

1

1 Answers

1
votes

The PostBackTrigger class is key to your solution, as it can be used to trigger the full page reload required for the download reponse to work. Downloads simply will not work from a partial postback.

However, as the buttons that should trigger the postback are inside your grid, using a single PostBackTrigger in the page markup is not enough, you need a specific trigger for each button / row.

Use something like this (call it from your Page_Load)

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