EvanGWatkins EvanGWatkins - 8 months ago 59
C# Question

Redirecting to another page after Response.End() has been called in C#

I am exporting a gridview to excel, using .Net 4.0 in a web application, on page load and need for the file to be generated and then the page to be redirected to the calling page. I am running into issues because my code to export to excel is as follows:

gvSummary.Style.Add("font-size", ".6em");
string attachment = "attachment; filename=filename.xls";
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvSummary.GridLines = GridLines.Horizontal;

I know that if I put the Response.Redirect() before the .End(), I will get redirected but the file is never generated, and if I put the Response.Redirect() after the .End() I get the file but no redirection.

The code written above works just fine in generating the file, however when after the file is generated I am still stuck seeing my Loading animation because I can not break out of the page. Any ideas?


I suggest to add a redirect header. Something like this:

Response.AddHeader("Refresh", "3; url=index.html");

Where 3 is time of the delay and index.html is url you need to redirect to