在 C# 中调用 Response.End() 后重定向到另一个页面

发布于 2024-09-18 13:42:55 字数 853 浏览 5 评论 0原文

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

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() 之前,我将被重定向,但文件永远不会生成,并且如果我将.End() 之后的 Response.Redirect() 我得到了文件,但没有重定向。

上面编写的代码在生成文件时效果很好,但是在生成文件后,我仍然无法看到加载动画,因为我无法跳出页面。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

红焚 2024-09-25 13:42:55

我建议添加一个重定向标头。像这样的:

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

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

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

苍风燃霜 2024-09-25 13:42:55

问题是 Response.End() 将页面的最后几位发送到客户端以完成请求。在您的情况下,请求包含该文件。

一种可能的解决方案是打开一个新页面进行下载并将重定向保留在当前页面中。

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.

墨离汐 2024-09-25 13:42:55

我将 target="_blank" 添加到将我带到该页面的链接中。因此,当它在新窗口中打开时,它将 Excel 工作表发送到浏览器,并关闭页面,因为在 Response.End(); 之后没有其他事情可做,

希望这会有所帮助。为我工作。

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.

苏辞 2024-09-25 13:42:55

您无法同时打开这两个文件,因此您可能需要尝试在新窗口中设置指向生成的 xls 文件的链接或将其设置为 iframe 的源

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

隔纱相望 2024-09-25 13:42:55

在 JavaScript 中:

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

按钮:

<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>

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>
许一世地老天荒 2024-09-25 13:42:55

我尝试通过 ScriptManager 打开一个单独的页面,该页面将在原始页面中进行重定向时下载我的文件。

所以我在主页中添加了以下代码:

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

然后在 CommonDownload 页面的 Page_Load 方法中添加了下载代码。我通过会话传递了 CommonDownload 页面中所需的一些信息。

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.

平生欢 2024-09-25 13:42:55

OnClientClick调用该方法会自动刷新。

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

OnClientClick call this method and it will automatically be refreshed.

 function PageRefresh() {
        setTimeout(function () {
            window.location.reload(1);
        }, 5000);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文