Link to home
Start Free TrialLog in
Avatar of rdbrunet
rdbrunetFlag for United States of America

asked on

How do I get ASP tofire off code when the user clicks the "X" box or close tab function on a web page?

My application tracks specific user data in its own "SESSION" table in the database.  When the user clicks the "Logoff" button, I have no problem clearing his/her entries from the session table.  I need to perform the same function when the user clicks the X at the top right corner of the browser or when they click the close X on the tab.  My first instinct was to create a Page_Unload method but I'm not sure what close event has to occur to call this.  I've also placed code in my Application_End and Session_End segments of the Global.asax file.  None of this seems to work.  My question: Is there a specific event that fires when the user closes a browser/window that I can attach my code to so that I can do a "clean" logoff of the user from the database?  I've researched this for two days and can't find a straight answer from anyone.  Thanks in advance.
Avatar of abhinayp86
abhinayp86
Flag of United States of America image

As far as i know, you can only get it via javascript.

works for 'X' or Alt+F4 or Tab
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function HandleOnClose() {
            try {
               alert("window is closing");
            } catch (err)
            { }
        }

    </script>

</head>
<body class="Resolution_1024x768" onunload="HandleOnClose()">
    <form id="form1" runat="server">

blah blah blah

</form>
</body>

Open in new window

Avatar of rdbrunet

ASKER

I don't think I can do what I need to do in javaScript.  I need to call a C# method - logoffSession() - that accesses a database service to delete the user record from the database.  I believe this call needs to be triggered by some event when the page/browser/tab closes.  I need to identify what that trigger is...
ASKER CERTIFIED SOLUTION
Avatar of abhinayp86
abhinayp86
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KinjalKumar Patel
KinjalKumar Patel

I will prefer the same way what abhinayp mentioned. As this is the common technique used in the industry when we want to handle this sort of situation.

Thanks,

Kinjal
This was applied as part of a solution that met our needs.