Solved

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

Posted on 2010-11-14
7
2,542 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
those links did not give me a solution
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now