Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to handle browser close event and abandon session in ASP.NET

Author: Jeff Nava
Download Source Code : 1433_BrowserCloseEvent.zip

Following article will show how we can handle browser close event and abandon the user session after closing browser window

Lots of people has the requirement to abandon or close the user session when a user closes browser window or tab so they need to handle the browser or tab close event to be handled in their code. We can't do it directly since http is stateless protocol. Let me show you the solution of this issue in ASP.NET.

Solution of this question is something simple. We can use two most popular events of HTML DOM that are used to detect browser window or tab close. These events are 'onunload' and 'onbeforeunload'. 'onunload' event might work well in some of the browsers and may not work well in some. On the other hand, 'onbeforeunload' event works nice in most of the popular browsers. So we can consider 'onbeforeunload' as better option for our solution.

Well, like all other things in this world, 'onbeforeunload' has not the perfection. When we will close a browser window or tab a message 'Are you sure you want to navigate away from this page? Press OK to continue, or Cancel to stay on the current page. Also, this will not work when a browser is closed by ending the process from task manager. There can be some other cases when this approach doesn't work but in many cases this approach is better and effective.

So, by using 'onbeforeunload', we can detect browser window or tab close and handle this our own logic for the web page.  I will show the code to handle browser close event and I will abandon the user session in it.
 

  1. Open Visual Studio 2010/2012
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Write below function in code file

    C#

    using System.Web.Services;
    
    [WebMethod]
    public static void AbandonSession()
    {
      HttpContext.Current.Session.Abandon();
    }
    

    VB.NET

    Imports System.Web.Services
    
    <WebMethod> _
    Public Shared Sub AbandonSession()
      HttpContext.Current.Session.Abandon()
    End Sub
    
     
  6. Add a script tag in between head tag of Default.aspx page and write following code
     

    <script type="text/javascript">
      function HandleOnclose() {
        alert("Close Session");
        PageMethods.AbandonSession();
      }
      window.onbeforeunload = HandleOnclose;
     script>

     

  7. Now you can see it in your browser. Close the tab and see the alert message. You can download the complete code from above.

     

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to check User Name and Email availability on Register Page in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net