views:

688

answers:

5

I have noticed that window.onunload event fires off AFTER page_load event which makes no sense.

This behaviour is creating an issue for me - in my unonload I clear the session, so if the Page_Load first BEFORE onunload, there are errors on the page displayed.

I would expect the javascript onunload to fire BEFORE Page_Load....is that the correct assumption?

TO CLARIFY: Let's assume I am on page test.aspx, then I click on the link that goes to the same page (say I click on a menu), what I observe is that Page_Load fires first, then onunload fires off. Makes no sense at all.

A: 

I would guess that window.unload is actually firing only when you're going to have to RENDER the new page you navigated to (aka the old DOM is being torn down in place of some new HTML). The browser doesn't know what to render until the response comes back from the server with the HTML to display. That HTML isn't generated until the page lifecycle completes, which includes Page_Load. Hence the page_load before the window.unload?

In any case, if you can clear the session during window.unload, why not just clear it in response to some user interaction and be a bit more explicit about it?

Edit: Could you also try window.onbeforeunload?

Eric
the problem is that i can't control the user. the user may choose to click wherever they want to. so if they click on a menu, i would expect that the window.onunload will fire first, clear the session, and then page_load will fire. not so.
gnomixa
I edited my answer - does window.onbeforeunload help your cause?
Eric
Thanks, but onbeforeunload creates more problems actually - it pops the dialogue that's not manually accessible for a programmer. I don't want the dialogue popped up, I just want the session to be cleared, THEN new page loaded. Arggg, apparently too much to ask for.
gnomixa
A: 

The onunload event does fire before the request for the new page is fired off to the server, so it definitely fires before the Page_Load method runs on the server.

The problem is most likely that you are sending another request to the server from the onunload event. As the IIS only handles one request at a time from each user, this request will be queued and executed after the request for the new page.

Guffa
I commented out the request to the server inside onunload, put break points inside Page_Load. Still, Page_Load executes first, then alert from window.unonload fires off......what gives? what's the point of window.unonload event then if it executes AFTER the new page has loaded.
gnomixa
Is there a way to TRULY clear session BEFORE loading the new page? I am totally lost and frustrated.
gnomixa
@guffa - I just wrote a little test app, and I don't think onunload fires BEFORE the request to the server. I'm skeptical myself - but the test app doesn't lie.
Eric
A: 

You can write an utility function which will handle removing of the session variables and then call that function in the respective menu click events. That should be simpler to use since window unload will fire after page load only.

Abhishek Rao
+1  A: 

Have you considered using a common base class for your pages, and clearing the session in there if the request isn't a postback (I assume that you're using session for postbacks)?

public class BasePage : System.Web.UI.WebControls.Page {
  protected override OnPreInit (EventArgs e) {
    // Get in nice and early, however you could use OnInit if you prefer
    if (!Page.IsPostBack) {
      Session.Clear();
    }          
}

Then your pages that need to clear session can be declared as:

public class SpecialPage : BasePage {
  // Your page logic goes here.
  // Note that if you need to do work in OnPreInit here you should call
  // base.OnPreInit(e) first.
}
Zhaph - Ben Duguid
A: 

It sounds like you are using the Session to save temporary variables that change from page to page. I would say the Session is not really suitable for this kind of scenario. A better solution would be to use the Httpcontext's Item collection which is scoped only on a per request basis. It's works just the same as the Session when storing data.

Context.Items["myvariable"] = "some data";

As it's only scoped on a per request basis, there is no need to use javascript to clear the items you have stored on each page request.

CogentasDeveloper - Jonathan