J
Jeff
I have some code that enables users to download the contents of a DataGrid
as an Excel file. The following code snippet is the relevant part. It works
just fine - meaning specifically that the downloaded Excel file is what I
want.
The problem is that every time this code runs, my "catch all" exception
handler (an HTTP Module) catches the "Thread was being aborted" exception
(from System.Threading.Thread.AbortInternal()).
What can I do to prevent that exception from being thrown? You can see that
the following code uses Response.End. is there a better way?
------------------- start of code snippet ---------------------
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");
// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw))
{
// instantiate a datagrid
dg.RenderControl(htw);
dgText = sw.ToString();
}
}
// set the response mime type
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();
------------------- end of code snippet ---------------------
Any ideas?
Thanks
as an Excel file. The following code snippet is the relevant part. It works
just fine - meaning specifically that the downloaded Excel file is what I
want.
The problem is that every time this code runs, my "catch all" exception
handler (an HTTP Module) catches the "Thread was being aborted" exception
(from System.Threading.Thread.AbortInternal()).
What can I do to prevent that exception from being thrown? You can see that
the following code uses Response.End. is there a better way?
------------------- start of code snippet ---------------------
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");
// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw))
{
// instantiate a datagrid
dg.RenderControl(htw);
dgText = sw.ToString();
}
}
// set the response mime type
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();
------------------- end of code snippet ---------------------
Any ideas?
Thanks