Link to home
Start Free TrialLog in
Avatar of mmalik15
mmalik15

asked on

how can we raise Session_End event when user closes the browser

i am using a Master page application & i am able to raise the Session_End event in Global.asax after user session is expired but i want to fire up the event if user closes the browser. I am using vb.net with framework 2.0
Avatar of slado2
slado2

The only chance is some Javascript that would call your handler via XmlHttpRequest object, but generally there's no reliable way to react when the user closes the browser.
Avatar of mmalik15

ASKER

Thanks for your comment.
I am not very good at java script. i have tried one of the script that does not seem to work. In my master page body element i added
<body onunload="window.location.href='TestB.aspx';">
and on  my TestB page i have following code

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   Call MyBase.OnLoad(e)
   ' End The Session
   Session.Abandon()

   ' Build A JavaScript String That Will Close This Web Browser Window
   Dim s As String = ""
   s &= "<script language=""javascript"">"
   s &= "window.close();"
   s &= "</script>"
   
   ' Add The JavaScript To The HTML Stream
   Page.RegisterClientScriptBlock("close", s)
End Sub

any ideas  how can we achieve it

Please explain why you need to invoke session.end, So I may suggest an alternative solution?
when a user logins to the application i get and store the start time and on session end i get the log off time. Now if some one closes the browser i need to log user off and get the log off time.
What i have done now is to add a confirm message box using javascript when user clicks the close button and i want to redirect the user to another page (using  window.location.href = "Test.aspx") which will call session.abondon. Butt the problem that i am facing in using that is when  user clicks ok on the confirm message box we cannot redirect to another page coz perhaps page is already unloaded
Javascript in my master page

<body id="htmlBody" runat="server" onbeforeunload="HandleOnClose()">

function HandleOnClose()
             {
    if (event.clientY < 0) {
      var result=event.returnValue = 'Are you sure you want to leave the page?';
           if (result)
           {
           
           }
           else
           {
      window.location.href = "TestB.aspx"
           
           }
     
                           }
             }

Any suggestions

A simple solution would be to reduce the Session time to 5 minutes, then you would find automatically that Session times are correct to within 5 minutes?
Was my last sugesstion acceptable or would you like me to look into alternative solutions?
thanks for asking
if you have an alternative solution that will be good
My final solution would be to use AJAX to callback to the server onUnload.

http://asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

Word of caution HTTP is Stateless and you cannot gauarentee that you will get this event to fire whatever technology you use. If your attempting to call a server side method as part of the client side dom unload event I would seriously encourage you to abandon this endeavour. You can not reliably capture this event. If i hit Alt+F4 in the browser the browser will close without sending the event to the server, thus you can't depend on the event triggering some server side code.
My bonus solution would be to abandon using Sesson_End to try and capture the end and add functionallity to your pages to update a "Last Activity time" for the current user. This could be done in a master page or a HttpModule, or other?

You would then use the session_end to take the value you recorded to the LastActivityTime and save as your session end.

This has only one drawback in that the Session end would represent the time they started looking at the final page not the time they stopped looking.

Does that make sense?
ASKER CERTIFIED SOLUTION
Avatar of slado2
slado2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial