Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a simple report. The problem is:

--I need to output the report on the webpage with a button that allows the data to have a "Save As" option

The ASPX page uses a "Submit" button to create data and display it below. It's simple. It works.
XML
<asp:Repeater runat="server" ID="rptTableView">
<ItemHeader>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "item1") %><td>
<td><%#DataBinder.Eval(Container.DataItem, "item2") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "item3") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "item4") %></td>
</tr>
</ItemHeader>
</asp:Repeater>


But when I try to use the "Generate Report" button:
C#
protected void btnGenerate_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/csv";
    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
    Response.Write(dataOut);//Global variable
    Response.End();
}

As expected: the Response.ContentType causes new data to be generated on the server and is pushed to the "User/Downloads" folder and NOT on the webpage.

I need the data generated to show on the page first and, if the user wants, allow that data to "Save As."

Help!
I've scoured the internet for the past week trying to do this.
Posted

1 solution

I wouldn't put this in the button click event, I'd make the button a link button and provide the report in a separate ASPX. Strip out everything from the ASPX page (except the very first line which references the server code).

Use this code in the page load event of the new page and make your link button call this page, passing any state that you need in a query string.

Here's a cut down sample of something similar that I did:

Page:
]]>

VB:
Response.Clear()
Response.AppendHeader("content-disposition", "attachment; filename=test.csv")
Response.ContentType = "TEXT/CSV"
Response.Write("Project ID, Project Name, Cost Estimate, Contact Name, Contact Numbers, Current Condition, How Identified" & vbNewLine)
Response.Write("R.14.001, Repair one, $3000, , , , " & vbNewLine)
Response.Write("S.14.003, Study one, $4500, , , , " & vbNewLine)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900