重定向到另一个页面到Response.End()已经在C#中被调用后 [英] Redirecting to another page after Response.End() has been called in C#

查看:511
本文介绍了重定向到另一个页面到Response.End()已经在C#中被调用后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导出一个gridview到excel,在Web应用程序中使用.NET 4.0,在页面加载和需要要生成的文件,然后重定向到调用页面的页面。我遇到的问题,因为我的代码导出到Excel如下:

  gvSummary.Style.Add(FONT-SIZE ,.6em); 
Response.Clear();
带安装=附件;文件名= filename.xls;
Response.ClearContent();
Response.AddHeader(内容处置,附件);
Response.ContentType =应用程序/ vnd.openxmlformats-officedocument.spreadsheetml.sheet
StringWriter的SW =新的StringWriter();
的HtmlTextWriter HTW =新的HtmlTextWriter(SW);
gvSummary.GridLines = GridLines.Horizo​​ntal;
gvSummary.RenderControl(HTW);
的Response.Write(sw.ToString());
到Response.End();



我知道,如果我把的Response.Redirect()的.END()之前,我会重定向但从未产生的文件,如果我把的Response.Redirect()的.END()后我得到的文件,但没有重定向。



上面写的代码在生成文件就好了,但是当生成文件后,我仍然坚持看我的加载动画,因为我不能打出来的页。任何想法?


解决方案

我建议增加一个重定向头。事情是这样的:

  Response.AddHeader(刷新,3; URL =的index.html); 



在哪里3是延迟和index.html的时间就是你需要重定向到

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());
    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

这篇关于重定向到另一个页面到Response.End()已经在C#中被调用后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆