Results 1 to 8 of 8

Thread: [RESOLVED] [2008] Executing code after Response.End()

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [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!

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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:
    1. Dim excelScript As String = "<script type=""text/javascript"">window.open('../UtilityPages/Loading.aspx?Page=../UtilityPages/Exports.aspx&FileName=" & _
    2.     Me.Page.Title.Replace(" ", "") & DateTime.Now.ToString("MMddyyy") & ".xls" & "','width=100,height=100,toolbar=no');</script>"
    3. 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.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [RESOLVED] [2008] Executing code after Response.End()

    Quote 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).
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [RESOLVED] [2008] Executing code after Response.End()

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

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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

  8. #8

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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
  •  



Click Here to Expand Forum to Full Width