1

hey hope someone can help me with this problem...

I have a page that loads when a student logs in, and at the top there's a label that's represents the last time he/she has logged in... This date is stored in an table

I tried using code that will update the date to todays date in the global.asax under

void Application_End(object sender, EventArgs e)
{

}

But seems like I cant use SQL connections there

How do I change the date stored in the table to the (todays.date) once the user closes the site by exiting?

Jaun Lloyd
  • 199
  • 2
  • 4
  • 13

4 Answers4

3

Application_End is not the right place for this. This is called when your whole application, not just a user session, shuts down.

The closest thing to what you want is probably Session_End. Beware, though, that this fires only after the user's session expires, which is usually around 20 minutes after the browser has been closed.

And no, unfortunately you cannot reliably detect that the user has closed his browser window. There are a few workarounds that might work most of the time, if you want to fiddle around with JavaScript:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
1

use Session_End instead Application_End

Application_End is Application specific and is common for the whole application and will fire once.

Session_End is user specific it fires once per user's scope..so right way would be to use this way..

Read more about these event here..

Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
0

Depending on how your authentication works (cookies, session-based etc), it may be easier to do this immediately after the user has successfully logged in.

If you are needing the last log in to be measured accurately, it makes sense to do it this way because Session_End may fire some time after they have actually finished using the site.

If you do wish to use Session_End, you could always store the time the user logged in in the session and then update the database when the Session_End event fires.

nick_w
  • 14,758
  • 3
  • 51
  • 71
0

You can use onbeforeunload event.

eg.

    <script type="text/javascript">
        window.onbeforeunload = function () 
        {
            //do something
        }
    </script>

This is the sample page with example : onbeforeunload Event Sample

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99