Solved

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

Posted on 2010-11-14
7
2,586 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

803 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