asp.net - user closes page need to reset session varaible...page_unload

Posted on 2011-05-07
Medium Priority
Last Modified: 2012-08-13
When the user closes the page I need to reset the session variable....isnt page_unload used for this when hit the 'X" on the top right hand corner??
Question by:GlobaLevel
LVL 13

Expert Comment

ID: 35713084
No, it isn't. The page_unload happens on the server side, while the page closes on the client side. There is no connection between both.

Implementing your requirement is a bit complicated. It needs a way the client (web browser / page) to inform your server side application that ALL your application pages have been closed. This can be done in several ways:

- Ajax request when the page closes: Whenever the user closes your page, it sends an ajax request to inform your app that the page has been closed and it will kill the session. But you will face a problem if there was several pages opened. The session will be killed if only one page is closed. You will need to find a way to fix this.

- Polling the server: I used this method in one of our web applications. It gives a good result for both, detecting who is online and using your app, and who went offline or closed you pages.
This approach consists of simply sending an ajax request from your page (client side) each interval (10 seconds maybe). The server receives this request and makes sure that the client has a page opened for your application. If the serve side app doesn't get a request for maybe 30 or more seconds, it can assume that all the pages to your application has been closed. The proble with this approach is that it will consider the page has been closed if the internet connection on the client side faced some trouble. This might cause some confusion for your user because the session expired although he didn't close the page.
LVL 83

Expert Comment

ID: 35716011

Accepted Solution

abhinayp earned 2000 total points
ID: 35720433
i dont think u can ever capture the close event from the serverside.

U can pop-open a new window from javascript, n in the page load of the poped up page, do ur coding n make the page close from there.

Additionally, U can refer to my answer here: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26385139.html

<head runat="server">

    <script type="text/javascript">
         function ConfirmClose() {
            if (event.clientY < 0) {
                //event.returnValue = 'Any message you want';


<body class="Resolution_1024x768" onbeforeunload="ConfirmClose()" >
<form id="form1" runat="server">

blah blah blah


********** page load of clearsession.aspx*********

    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
              //ur Coding
        ClientScript.RegisterStartupScript(this.GetType(), "refresh", "ClosePage();", true);

### ClosePage() function in clearsession.aspx###
            function ClosePage() {
                window.open('', '_self', '');


Open in new window


Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question