Solved

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

Posted on 2010-11-14
7
2,661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

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'…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Suggested Courses

624 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