|
-
Aug 21st, 2008, 09:53 AM
#1
Thread Starter
Registered User
[RESOLVED] [2008] Executing code after Response.End()
I am exporting a DataTable to Excel and while Excel COM does its job about as fast as I could handwrite the worksheet, I'm redirecting users to a "Loading" page.
Excel export:
Code:
' Write stuff to Excel, etc. etc. then...
HttpContext.Current.Response.WriteFile(path)
HttpContext.Current.Response.End()
A portion of my loading page JavaScript:
Code:
<body onload="BeginPageLoad();" onunload="EndPageLoad();">
// Then the JavaScript
function BeginPageLoad() {
// Send the user to the Excel page after it is ready.
location.href = '<%= Request.QueryString("Page") & "?FileName=" & Request.QueryString("FileName") %>';
// Just a counter to show progress to the user.
iIntervalId = window.setInterval("iLoopCounter=UpdateProgress(iLoopCounter, iMaxLoop)", 500);
}
function EndPageLoad() {
window.clearInterval(iIntervalId);
self.close();
}
Self.close() does not execute after writing the Excel file to the Response. However, if I just test by putting the Thread to sleep, it works fine. This is because Response.End() is ending execution correct?
1) Can I finish writing the Excel file with something else besides Response.End() and if so, is that even good practice?
2) Can I somehow otherwise close my loading page after done writing the Excel file?
I think I've provided the relevant code, but let me know if I should show more. Thanks!
-
Aug 21st, 2008, 03:09 PM
#2
Re: [2008] Executing code after Response.End()
There is no need to use Response.End().
What I would do is setup a Generic Handler (ashx file). The only code in this file would be to do the WriteFile.
Then you just redirect your users to that page after the loading page (why is there even a loading page?).
Not sure why you're trying to close though. If the browser is told to go someplace and you tell it to close, then it's going to do one or the other (in your issue, just go). My suggestion is don't have the browser close at all. It's a bad user experience and no matter what, your browser is going to tell the user that "the page is requesting to close the windows. Y or N?".
If you're trying to mimic sites that seemingly open a new window then close it when a file is loading, this is because they have a hyperlink directly to the file to download (with target="_blank") and the browser attempts to navigate to this page as usual then sees that it should be download the file instead and the window closes.
-
Aug 21st, 2008, 03:54 PM
#3
Thread Starter
Registered User
Re: [2008] Executing code after Response.End()
Thanks for the response.
I've never used a Generic Handler file before, but I'll look into it.
If you're trying to mimic sites that seemingly open a new window then close it when a file is loading, this is because they have a hyperlink directly to the file to download (with target="_blank") and the browser attempts to navigate to this page as usual then sees that it should be download the file instead and the window closes.
Essentially what I'm trying to do, but I'm creating the Excel file first and wanted to provide some visual feedback on my intermediate "Loading" page while the third "Excel" page is doing the work - all the while doing this in an attempt to not tie up the "Main" page that the user is on.
i.e. When the user wants to Export, do this:
vb.net Code:
Dim excelScript As String = "<script type=""text/javascript"">window.open('../UtilityPages/Loading.aspx?Page=../UtilityPages/Exports.aspx&FileName=" & _
Me.Page.Title.Replace(" ", "") & DateTime.Now.ToString("MMddyyy") & ".xls" & "','width=100,height=100,toolbar=no');</script>"
Me.ClientScript.RegisterStartupScript(Me.GetType(), "ExcelScript", excelScript)
Then the "Loading" page takes over, sets is location.href to the "Excel" page that is in the QueryString that does the work and meanwhile the "Loading" page lets the user know with some visual indicator. When the work is done the Open/Save/Cancel dialog shows, the "Loading" popup closes and the user is left just with the "Main" page that was never interrupted.
Am I off my rocker?
I'm probably going to just forget the intermediate loading page and just open a new window that directly does the Excel work and let it sit there looking ugly when everything is done.
EDIT: That's exactly what I'm doing. Forget the loading thing. It looks like I wrote it in 1997.
Last edited by nmadd; Aug 21st, 2008 at 04:01 PM.
-
Aug 22nd, 2008, 01:36 PM
#4
Re: [RESOLVED] [2008] Executing code after Response.End()
From the page where the user, say, clicks the button to generate the Excel file, Response.Redirect the user to a filedownloader.aspx page which directly does the Response.WriteFile() so that the user is prompted without actually leaving the page where he clicked the button in the first place.
-
Aug 22nd, 2008, 01:43 PM
#5
Re: [RESOLVED] [2008] Executing code after Response.End()
 Originally Posted by mendhak
From the page where the user, say, clicks the button to generate the Excel file, Response.Redirect the user to a filedownloader.aspx page which directly does the Response.WriteFile() so that the user is prompted without actually leaving the page where he clicked the button in the first place.
This is a good suggestion but I would replace an aspx page with an ashx handler. It would be more efficient (aspx has additional overhead).
-
Aug 22nd, 2008, 02:07 PM
#6
Thread Starter
Registered User
Re: [RESOLVED] [2008] Executing code after Response.End()
 Originally Posted by mendhak
From the page where the user, say, clicks the button to generate the Excel file, Response.Redirect the user to a filedownloader.aspx page which directly does the Response.WriteFile() so that the user is prompted without actually leaving the page where he clicked the button in the first place.
Thanks . I'll give this a shot.
-
Aug 22nd, 2008, 02:16 PM
#7
Re: [RESOLVED] [2008] Executing code after Response.End()
What kasracer said, rightly so. The ASHX was created for this very purpose. I shouldn't cater to those .NET 1.1ers
-
Aug 22nd, 2008, 05:49 PM
#8
Thread Starter
Registered User
Re: [RESOLVED] [2008] Executing code after Response.End()
Right on VBFers. You've been repified.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|