Clear Session on page termination

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I want to clear some specific Session variables when the user closes a page.
On my page I have a "return to ..." button which navigates back to the
parent page from which it was invoked - under the click event of this button
I clear my Session variables.

However, how can I capture the fact that the page has been finished with, to
clear my session variables, if the user (a) navigates away from the page
directly by entering a URL, (b) closes the window manually or (c) uses the
browser's BACK button to go back to the calling page ?

Basically I need a generic way to capture page termination, to invoke my
memory cleanup.
 
Good morning,
We ran into a problem like this too but did not find a
way to do it during termination. Ie. BAck btn, URL
address chg or hitting the 'X' on the browser.

What we ended up doing was checking when the page is
loaded at the begining. Something like;
If (Session.Count > 0) Then
Session.Clear()
End If
Then you know session is empty.

Thanks
Deasun
PS. If you find out a way let us know. Thanks.
 
One way you could do this, is by using framesets. Create a single frame
frameset, with the source set to your page. In the "onUnload" handler of
the frameset, use javascript to pop open another window, which will execute
a page to clear the session variables, and close itself when done.

In the head section of your frame page, you would have a script:

<script language="javascript">
function window_onunload()
{
window.open("/clearsession.aspx", "clearsession",
"height=100,width=100,status=no,toolbar=no,menubar=no,location=no");
}
</script>

Your frameset would have only a single page. When the frame is unloaded, it
will open the popup "clearsession.aspx" which can clear the session. We
don't put the onunload handler in the body, as we don't want to clear the
session when the page unloads for postbacks, etc - only when the user
navigates to a different URL, closes the window, or navigates back to the
parent page using the supplied link (which would target _top)

<frameset rows="100%" onUnload="window_onunload()">
<frame name="MainFrame" src="MyPage.aspx" marginwidth="0"
marginheight="0" scrolling="auto" frameborder="0">
</frameset>

The "clearsession.aspx" would simply do your memory clean up, and run
"<script>self.close</script>" on the client side to close itself when done.
This page should execute pretty quickly, and be almost transparent to the
user.

One possible problem with this, is that if the page being returned to loads
before clearsession.aspx has run, the session won't be cleared. You could
double check for this, by also checking the session in the page_load of the
page being returned to, and if the session is not clear, then clear it.

It's a bit of an awkward way to do things, but it might help achieve your
goal :-)

Hope this helps,

Mun
 
Back
Top