Solved

How to detect browser close in ASP.NET wth master pages, login form

Posted on 2010-11-14
7
2,596 Views
Last Modified: 2012-05-10
I have a login form, a master page and quite a few aspx pages.
I need to detect when the user coses the browser by clicking the x at the far top right.

I add the following code in my master page

<body  onunload="logUser();">
<script type="text/javascript" language="javascript">
    function logUser() {
        debugger;
        __doPostBack('ctl00$btnXClick', '');
    }
    </script>
 
</head>

Also I added a button with  no display on the master page
and in the code behind of the master page I added the btnclick event

Protected Sub btnXClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnXClick.Click
        Try
           do something
        Catch ex As Exception
            ex.ToString()
        End Try
     
    End Sub

Now when I login to the login.aspx , the onunload is executed, my expectation was that this event will only occur when the "X" in the browser is clicked, when I want to close the browser

0
Comment
Question by:countrymeister
  • 3
  • 2
7 Comments
 
LVL 3

Expert Comment

by:xeonol
ID: 34134456
You may want to use onbeforeunload, try <body  onbeforeunload="logUser();"> to see if it works for you.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34134777
More likely you need to design your application so you do NOT need to know the user closed the browser

http://forums.asp.net/p/1094920/1658155.aspx
http://bytes.com/topic/asp-net/answers/288833-how-detect-browser-close-event
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34135775
xeonol:
The onbeforeunload does not help either and from what I see only works in IE

mplungjan:
I do need to know when the browser is closed.
Have you investigated using Page Methods to invoke server side calls or calling a web service to send out a request on close of a browser.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 34135847
No, I have never needed to know when a browser is closed. It is not an event that is triggered in an orderly fashion so it cannot be relied upon
0
 
LVL 1

Author Comment

by:countrymeister
ID: 34136755
mplungjan:

I have a system where I need to see active users, so I created a web page to see the logged in users
I record the login when they sign on and logout time when they sign off or the session ends

When the close the browser I need to log them off, (update their logout timestamp)
Hence I need to trap the browser close click
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 34141863
those links did not give me a solution
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 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