Click here to Skip to main content
13,732,072 members
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 23 Jun 2005
Licenced Public Domain

Detect browser closing through clicks on the [X] button

Rate this:
Please Sign up or sign in to vote.
This explains the technique to detect Window closing with C# and how to close user sessions safely at the same time.


I had been searching through the internet to find out how to control this issue: You have a web application where you validate every user that logs in, if somebody tries to log in from another place with the same user name, he should be denied access because the user is at the system right at that moment.

Well, the way I'm controlling this, is using a table where I put all the users’ info. I’ll make this simple. Look at the following example:

Table : employee

Notice that if the user logs in the application, the field "active" will turn to 1… if the user logs off, the field will turn to 0.

Well ... I have this web application where I have a button to sign out. When I press the button, it invokes a function to update my database to turn “active”=0, but the problem is what if I press the X button of the navigator? I mean, there’s no way to detect using C#, how to catch when I close the browser in order to update the database, right? Everyone told me that the event onUnload (C#) works for detecting it… wrong! This event is not triggered when I close the window but it does when I browse another web page. So it doesn't help me. Others told me that Global.Asax catches a window closing with Session_End.. Wrong! Global.Asax works only when you tell the application to end or when the session expires … Believe me.. I tried a lot of things…

Well I guess I have found a solution to this problem… I took a long time searching though the internet looking for something until I got this link and based on the answer provided in that link, I decided to do this. I have Login.aspx, RETURN.aspx (it has three pages, too, left and principal, it's a frames page…) and the spyWin.aspx , (special thanks to PE_CF_DEV from experts-exchange for the code, I just took your code and mixed it with C#). Work with two session variables, one to save the user login (Session[“USER”]) and the other to check if it's the right moment to update the database (Session[“RETURN”]).

  1. In your login page …login.aspx, you have to put this in the LOAD event:
    if ( Convert.ToString(Session["USER"]).Length > 0 )
        if (Session["RETURN"]!=null)
            if ( Convert.ToInt32(Session["RETURN"]) == 1)
                Session["RETURN"] = 0;
                …<Code to reset database>…

    This will first check if I had logged in recently, if the session is still alive and is trying to enter at Login.aspx, it will be redirected to RETURN.aspx.

  2. Put this code in the pressed event of the button to log out (this button is located in the left frame):
    Response.Write("<script>javascript: parent.opener=''; " + 

    The first line closes the session, this is caught by the event Session_end of GLOBAL.ASAX.

    protected void Session_End(Object sender, EventArgs e)
        if ( Convert.ToString(Session["USER"]).Length > 0 )
            <… Your code to update database>
            Session["USER"] = null;

    The second one, invokes a JavaScript event to close the window. Usually the browser detects when you close it, it sends you a message (message “do you want to close this window?”). The next point will solve this so that no message window appears.

  3. Put this in the LOAD event of RETURN.aspx (not the pages that form the frames page, I mean, not the left, top, or the principal page).
    if (Session["USER"]==null)
    else { Session["RETURN"] = 0; }

    This will be validated if there's still a user with an active session. I’ll explain a little later, the use of Session["RETURN"].

    At the moment, I can only say that this session variable is initialized.

  4. Put this in the HTML section (in the <HEAD> ) of Index.aspx ( not the pages that form the frames page, I mean, not the left, top, or the principal page).
    <script language=javascript>
    function launch_spyWin() 
        spyWin = open('spyWin.aspx','spyWin',
    onunload = launch_spyWin;

    This will solve the point explained in step 2). My code is based on the code from PE_CF_DEV.

  5. With JavaScript we know that the event “onUnload” will run the function regardless of the page being closed or the user navigating to another page. OK, now write this in the LOAD event of spyWin.aspx.
    if (Convert.ToInt32(Session["RETURN"])==0)
        Session["RETURN"] = 1 ;
        string script = "<script type='text/javascript' " + 
                "language='javascript'> " +
                " function check_opener() { " +
                " if (opener && opener.closed){ " +
                " parent.opener=''; " +
                " parent.close(); " +
                "'Login.aspx') " +
                " } " +
                " else{ " +
                " parent.opener=''; " +
                " parent.close(); " +
                " } " +
                "} " +
                " onload = function() { " +
                " self.blur(); " +
                " setTimeout('check_opener()',0); " +
                " } " +
                " </script>";
        this.Controls.Add(new LiteralControl(script));
        Session["RETURN"] = 0 ;

    This spyWindow is a non-visible popup window that will be triggered when the main window is closed. When I change Session["RETURN"] = 1, the Login.aspx catches this variable and then updates the database (the same function of Session_End of Global.asax)..

Problem solved! Now my life is easier...

Hope this helps someone…


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Mario Serrano (makadown)
Web Developer
Mexico Mexico
Mario Serrano is a C# Developer from Mexicali , Baja California. Coding for food...

He was working for Baja California´s Government, developing with Power Builder and Oracle Databases. Now He's currently learning more with C# in his actual job and willing to increase knowledge about .NET Big Grin | :-D .

You may also be interested in...

Comments and Discussions

QuestionDIfferent Browser Pin
cjdg084-Oct-18 19:29
membercjdg084-Oct-18 19:29 
QuestionUna duda con Chrome Pin
El Barrio 1312-Nov-13 8:02
memberEl Barrio 1312-Nov-13 8:02 
GeneralMy vote of 5 Pin
ysea23-Sep-12 23:56
memberysea23-Sep-12 23:56 
GeneralNeed help.......... Pin
Ajay Kale New2-Sep-10 0:22
memberAjay Kale New2-Sep-10 0:22 
Generaltry this it will help u more Pin
santosh d25-Mar-10 19:17
membersantosh d25-Mar-10 19:17 
Generalneed some help. Pin
7-Jul-08 16:44
member7-Jul-08 16:44 
Generalneed some help. Pin
7-Jul-08 16:44
member7-Jul-08 16:44 
GeneralThank you Pin
HAWK@INDIA24-Jun-08 2:18
memberHAWK@INDIA24-Jun-08 2:18 
QuestionIndex, spywin and return pages?? Pin
danolu16-Oct-07 10:44
memberdanolu16-Oct-07 10:44 
QuestionThis doesn't seems to work in mozilla. Any Solution? Pin
manoj pal15-Jun-07 3:41
membermanoj pal15-Jun-07 3:41 
GeneralUpdate Database Pin
atogwe11-Jun-07 7:36
memberatogwe11-Jun-07 7:36 
GeneralUpgraded Article coming soon Pin
Mario Serrano (makadown)21-Mar-06 16:39
memberMario Serrano (makadown)21-Mar-06 16:39 
GeneralI figured out how not to open up a popup Pin
SikeMullivan10-Dec-05 18:33
memberSikeMullivan10-Dec-05 18:33 
GeneralRe: I figured out how not to open up a popup Pin
SikeMullivan10-Dec-05 20:10
memberSikeMullivan10-Dec-05 20:10 
QuestionWhat about popup blocker Pin
Imran Salahuddin16-Sep-05 5:59
memberImran Salahuddin16-Sep-05 5:59 
QuestionWhat about popup blocker Pin
Imran Salahuddin16-Sep-05 5:58
memberImran Salahuddin16-Sep-05 5:58 
GeneralBad application design Pin
Harky30-Jun-05 8:05
memberHarky30-Jun-05 8:05 
GeneralRe: Bad application design Pin
SikeMullivan10-Dec-05 15:57
memberSikeMullivan10-Dec-05 15:57 
GeneralBrowser closing pressing X Pin
Fregate28-Jun-05 17:46
memberFregate28-Jun-05 17:46 
GeneralRe: Browser closing pressing X Pin
Ian Powell30-Jun-05 6:02
memberIan Powell30-Jun-05 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 23 Jun 2005
Article Copyright 2005 by Mario Serrano (makadown)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid