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

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

问题描述

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

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

我知道如果我将 Response.Redirect() 放在 .End() 之前,我将被重定向但文件永远不会生成,如果我将 Response.Redirect() 放在 .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");

其中 3 是延迟时间,index.html 是您需要重定向到的 url

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

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

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