Response.Write/Flush/End

G

Guest

Hi

I've created a generic 'Report' class that takes a DataView from a DataGrid control and writes it out to the response as a csv file for download. Basically, when a user clicks the download button on the page, this class is instantiated and the download method called. The odd thing is you get the Save/Open dialog popup twice!! Any ideas why? The code look like

public void DownloadDataView(DataView view, Page page

string fileName = view.Table.TableName.ToString()

if (fileName == ""

fileName = "report"


page.Response.BufferOutput = true
page.Response.Clear()
page.Response.ClearContent()
page.Response.ClearHeaders()

page.Response.ContentType = "application/octet-stream"
page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".csv\"")

page.Response.Flush()

//write out column heading
int i = view.Table.Columns.Count

for(int j = 0; j<i; j++

if (j == i -1

page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString()
Replace("\"", "'") + "\"\n")

els

page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString(
.Replace("\"", "'") + "\",")



page.Response.Flush()

//write out actual dat
foreach(DataRow row in view.Table.Rows

for(int j = 0; j<i; j++


if (j == i -1

page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\"\n")

els

page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\",")




page.Response.End()


Thanks for your help

Dan
 
P

Patrice Scribe

Never saw this behaviour. I would comment the Flush calls (and even perhaps
the End call) to see what happens.

Patrice


--

Dan said:
Hi,

I've created a generic 'Report' class that takes a DataView from a
DataGrid control and writes it out to the response as a csv file for
download. Basically, when a user clicks the download button on the page,
this class is instantiated and the download method called. The odd thing is
you get the Save/Open dialog popup twice!! Any ideas why? The code look
like:
public void DownloadDataView(DataView view, Page page)
{
string fileName = view.Table.TableName.ToString();

if (fileName == "")
{
fileName = "report";
}

page.Response.BufferOutput = true;
page.Response.Clear();
page.Response.ClearContent();
page.Response.ClearHeaders();

page.Response.ContentType = "application/octet-stream";
page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".csv\"");

page.Response.Flush();

//write out column headings
int i = view.Table.Columns.Count;

for(int j = 0; j<i; j++)
{
if (j == i -1)
{
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString().
Replace("\"", "'") + "\"\n");
}
else
{
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString()
.Replace("\"", "'") + "\",");
}
}

page.Response.Flush();

//write out actual data
foreach(DataRow row in view.Table.Rows)
{
for(int j = 0; j<i; j++)
{

if (j == i -1)
{
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\"\n");
}
else
{
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\",");
}
}
}

page.Response.End();
}

Thanks for your help.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top