强制回发Asp.Net
请看看下面的click事件...
Please take a look at the following click event...
Protected Sub btnDownloadEmpl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownloadEmpl.Click
Dim emplTable As DataTable = SiteAccess.DownloadEmployee_H()
Dim d As String = Format(Date.Now, "d")
Dim ad() As String = d.Split("/")
Dim fd As String = ad(0) & ad(1)
Dim fn As String = "E_" & fd & ".csv"
Response.ContentType = "text/csv"
Response.AddHeader("Content-Disposition", "attachment; filename=" & fn)
CreateCSVFile(emplTable, Response.Output)
Response.Flush()
Response.End()
lblEmpl.Visible = True
End Sub
这code简单地从出口数据表的数据到CSV文件。这里的问题是lblEmpl.Visible =真从来没有被击中,因为这code不导致回发到服务器。即使我把code lblEmpl.Visible线=真正的click事件顶部的行执行罚款,但页面永远不会更新。我该如何解决这个问题?
This code simply exports data from a datatable to a csv file. The problem here is lblEmpl.Visible=true never gets hit because this code doesnt cause a postback to the server. Even if I put the line of code lblEmpl.Visible=true at the top of the click event the line executes fine, but the page is never updated. How can I fix this?
这行:
lblEmpl.Visible = True
从未被击中,因为这一行:
Never gets hit because this line:
Response.End()
抛出 ThreadAbortException
我觉得处理这一个更清洁的方法是创建一个简单的HttpHandler的组件,然后在弹出窗口中的开放了。 (弹出窗口不应实际打开。在大多数情况下,浏览器会意识到这实际上是一个下载,并将坐席preSS的标签/窗口。)
I think a cleaner way to handle this is to create a simple HttpHandler component, and 'open' it in a popup window. (The popup window shouldn't actually open. In most cases the browser will realize it's actually a download, and will suppress the tab/window.)
研究了的IHttpHandler
接口。它们实际上是实现非常简单。
Research the IHttpHandler
interface. They're actually quite simple to implement.
下面是一个示例处理程序。对不起,这一段时间了,我被称为成一个会议:
Here's a sample handler. Sorry it took awhile, I got called into a meeting:
public class CensusHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string fileName = String.Format(
CultureInfo.CurrentUICulture,
"E_{0:00}{1:00}.csv",
DateTime.Today.Month,
DateTime.Today.Day
);
context.Response.ContentType = "text/csv";
context.Response.AddHeader(
"Content-Disposition", String.Format(null, "attachment; filename={0}", fileName)
);
//Dump the CSV content to context.Response
context.Response.Flush();
}
public bool IsReusable { get { return false; } }
}
OK,尝试添加一个JavaScript onclick事件来触发下载:
OK, try adding a javascript onclick event to trigger the download:
<asp:Button ID="Clickety" runat="server" Text="Click Me!" OnClick="Clickety_Click"
OnClientClick="window.open('Handler.ashx', 'Download');" />
常规的OnClick
事件将触发回传code。的JavaScript的onclick(的OnClientClick
)事件将启动通过下载的HttpHandler
。
The regular OnClick
event will fire your postback code. The javascript onclick (OnClientClick
) event will launch the download via the HttpHandler
.