开发者

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");
    Response.Clear();
    string attachment = "attachment; filename=filename.xls";
    Response.ClearContent();
    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;
    gvSummary.RenderControl(htw);
    Response.Write(sw.ToString());
   开发者_Go百科 Response.End();

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


The problem is Response.End() send the last bits of the page to the client in order to complete the request. In your case, the request contains the file.

One possible solution is be to open a new page for the download and keep the redirect in the current page.


I added the target="_blank" to the link that takes me to the page. So when it opens in a new window, it send the excel sheet to the browser, and closes the page as it has nothing else to do after the Response.End();

Hope this helps. Worked for me.


You can't do both open so you might want to try setting the link to the generated xls file in a new window or as source to a iframe


In JavaScript:

function refresh() { document.location.reload(true); }

button:

<td valign="top" style=" width:100px ">
<asp:Button runat="server" ID="btnX" Text="X" CssClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text ui-state-hover" OnClientClick="refresh();"  Width="180px" />
</td>


I tried opening a separate page through ScriptManager that will download my file while doing the redirect in my original page.

So I added below code in my main page:

string strScript = "<script language='javascript'> window.open('CommonDownload.aspx','Report');</script>";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "clientScript", strScript, false);
Response.Redirect("Page.aspx");

And then added the download code in the Page_Load method of my CommonDownload page. I passed some of the info I needed in the CommonDownload page through through the session.


OnClientClick call this method and it will automatically be refreshed.

 function PageRefresh() {
        setTimeout(function () {
            window.location.reload(1);
        }, 5000);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜