How to implement a download for dynamic files in asp.net with masterpages
- by Tim
Hello,
the title says it all. I have seen some similar questions on SO like this or this, but either i have overlooked something or my requirement is different, neither works.
My situation is following:
i have a Masterpage
one of its contentpage is called MasterData.aspx
MasterData has an asp.net ajax tabcontainer control with one usercontrol in every tabpanel
these usercontrols(f.e. MD_Customer.ascx)hold the main content(like a normal page)
they all have GridViews in it and i want to provide an Excel-Export-Button
What i've tried is is to use an iframe like here. But the function that adds the iframe to the document gets never called and therefore i never see the save-as-dialog. Maybe this is caused by using a MasterPage.
Does somebody has an idea on how to provide a button in an UpdatePanel that causes an async postback, so that i can generate a CSV dynamically in codebehind and write it to the response?
Thank you in advance.
aspx-markup:
<asp:UpdatePanel ID="UpdGridInfo" runat="server" >
<ContentTemplate>
<asp:Label ID="LblInfo" Font-Underline="false" runat="server" CssClass="content" ></asp:Label>
<asp:ImageButton ToolTip="export to Excel" style="vertical-align:bottom" ID="BtnExcelExport" ImageUrl="~/images/excel2007logo.png" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
and the BtnExportExcel codebehind handler(of course it cannot work to write the csv to the response of this page):
Private Sub BtnExcelExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnExcelExport.Click
Dim csv As String = tableToCsv(DirectCast(Me.GridSource, DataTable))
Response.AddHeader("Content-disposition", "attachment; filename=RuleConfigurationFile.csv")
Response.ContentType = "application/octet-stream"
Response.Write(csv)
Response.End()
End Sub