在 ASP.NET 中将文件写入响应后回发不起作用 [英] Post Back does not work after writing files to response in ASP.NET

查看:19
本文介绍了在 ASP.NET 中将文件写入响应后回发不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么?

我有一个 ASP.NET 页面,它允许用户通过单击按钮下载文件 a.用户可以从可用文件列表(RadioButtonList)中选择他想要的文件,然后单击下载按钮进行下载.(我不应该为可以下载的每个文件提供链接 - 这是要求).

I have an ASP.NET page which allows the user to download file a on a button click. User can select the file he wants from a list of available files (RadioButtonList) and clicks on download button to download it. (I should not provide link for each file that can be downloaded - this is the requirement).

我想要什么?

我希望用户通过选择所需的单选按钮并点击按钮来一个一个下载多个文件.

I want the user to download multiple files one by one by selecting the required radio button and clicking on the button.

我面临什么问题?

我第一次可以正常下载文件.但是,下载后,如果我选择其他文件并单击按钮下载它,按钮的单击事件不会回传,也不会下载第二个文件.

I can download the file for the first time properly. But, after downloading, if I select some other file and click on the button to download it, click event of the button does not post back and the second file will not be downloaded.

我在按钮点击事件上使用以下代码:

I use the following code on the button click event:

protected void btnDownload_Click(object sender, EventArgs e)
{
    string viewXml = exporter.Export();
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=views.cov");
    Response.AddHeader("Content-Length", viewXml.Length.ToString());
    Response.ContentType = "text/plain";
    Response.Write(viewXml);
    Response.End();
}

我在这里做错了吗?

同样的问题可以在 IE6、IE7 和 Chrome 中复制.我认为这个问题与浏览器无关.

Same problem can be replicated in IE6, IE7 and Chrome. I think this problem is browser independent.

推荐答案

我在使用 sharepoint 时遇到了同样的问题.我在页面上有一个发送文件的按钮,单击该按钮后,表单的其余部分没有响应.原来它是一个共享点的东西,它将变量 _spFormOnSubmitCalled 设置为 true 以防止任何进一步的提交.当我们发送文件时,这不会刷新页面,因此我们需要手动将此变量设置回 false.

I had this same issue with sharepoint. I have a button on the page that sends a file and after clicking the button, the rest of the form was unresponsive. Turns out it is a sharepoint thing that sets the variable _spFormOnSubmitCalled to true to prevent any further submits. When we send a file this doesn't refresh the page so we need to manually set this variable back to false.

在 webpart 中的按钮上,将 OnClientClick 设置为页面 javascript 中的函数.

On your button in the webpart set the OnClientClick to a function in your javascript for the page.

 <asp:Button ID="generateExcel" runat="server" Text="Export Excel" 
OnClick="generateExcel_Click" CssClass="rptSubmitButton"
OnClientClick="javascript:setFormSubmitToFalse()" />

然后在javascript中我有这个功能.

Then in the javascript I have this function.

function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
    return true;
}

我发现 3 秒的停顿是必要的,否则我会在 sharepoint 设置之前设置变量.通过这种方式,我让 sharepoint 正常设置,然后立即将其设置回 false.

The 3 second pause I found was necessary because otherwise I was setting the variable before sharepoint set it. This way I let sharepoint set it normally then I set it back to false right after.

这篇关于在 ASP.NET 中将文件写入响应后回发不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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