This line:
lblEmpl.Visible = True
Never gets hit because this line:
Response.End()
Throws a ThreadAbortException
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.)
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, 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');" />
The regular OnClick
event will fire your postback code. The javascript onclick (OnClientClick
) event will launch the download via the HttpHandler
.