Search code examples
asp.netpdfcrystal-reportsexport

ExportToHttpResponse is not working for Itemtemplate button inside update panel in ASP.net


I have a Gridview inside an UpdatePanel. I want to download file by clicking on the itemtemplate button inside the gridview. Every thing is working fine when the update panel is removed.the file is downloading smoothly but inside updatepanel file is not downloading. i want to download the file within updatepanel. what should i do? here my code.

protected void ibPrint_Click(object sender, ImageClickEventArgs e)
{          
    GridViewRow clickedRow = ((ImageButton)sender).NamingContainer as GridViewRow;
    string File1 = gvBRV.DataKeys[clickedRow.RowIndex].Values[1].ToString();
    crystalReport = obj3.Rpt_Voucher(0, "Files", Server.MapPath(@"CrystalReports\Attachments\Files.rpt"),File1);
    Response.Buffer = false;
    Response.ClearContent();
    Response.ClearHeaders();
    crystalReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "CashReceipt.pdf");
    crystalReport.Refresh();
    lblMsg.Text = "Export Successfully.";        
}


 <asp:GridView ID="gvBRV" runat="server" AutoGenerateColumns="False" DataKeyNames="Files">
    <Columns>
        <asp:TemplateField HeaderText="Export" HeaderStyle-ForeColor="Black">
            <ItemTemplate>
                <asp:ImageButton ID="Export" ClientIDMode="AutoID" class="imagebutton" runat="server"
                    ImageUrl="~/images/print.jpg" Width="25px" Height="25px" ImageAlign="Middle"
                    EnableTheming="True" Enabled="False" OnClick="ibPrint_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Solution

  • You need to make a PostBack Trigger for button Export

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
    
            <asp:ImageButton ID="Export" runat="server" />
    
        </ContentTemplate>
        <Triggers>
    
            <asp:PostBackTrigger ControlID="Export" />
    
        </Triggers>
    </asp:UpdatePanel>
    

    Update:

    I see the button is in a GridView. In that case adding a trigger to the UpdatePanel directly does not work. You need to add it in code behind.

    foreach (GridViewRow row in gvBRV.Rows)
    {
        ImageButton ib = row.FindControl("Export") as ImageButton;
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(ib);
    }