Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-14
7
Medium Priority
?
2,750 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1500 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Suggested Courses

916 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