By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Response.Write/Flush/End

P: n/a
Dan
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
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


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

Patrice
--

"Dan" <an*******@discussions.microsoft.com> a écrit dans le message de
news:54**********************************@microsof t.com...
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


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.