1

I use c# asp.net 4 web.forms.

I have a page (lets called it X) I need execute a method on X in this situation only:

  • When User visit another URL, directly input in the Browser or clicking another link on the Page.
  • When User Log Out from my Web Application.
  • When a User Close the Browser.

At the moment I'm trying Page_Unload Event but as for MSDN "Occurs when the server control is unloaded from memory" and I'm not able to make it works as expected.

Looking in the documentation I cannot find a suitable Event for my Page.

What are the possible alternative solutions? Thanks for your time on this.

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 1
    Possible duplicate: http://stackoverflow.com/questions/6482478/detect-the-browser-closing-event-of-altf4-in-asp-net ; http://stackoverflow.com/questions/5798964/detect-browser-close-events-in-vb-net ; http://stackoverflow.com/questions/4458157/how-to-close-all-threads-on-browser-close-event – Smudge202 Sep 30 '11 at 09:22
  • Thanks to point out, buy my questions is different. Your links could present a possible solution to my problem, I will have a look. – GibboK Sep 30 '11 at 09:40

4 Answers4

3

This simply isn't possible.

HTTP requires browsers to request resources from the server first - without this the server cannot do anything. Closing your browser will not request anything from the server.

Your only option is to use session cookies, which are wiped when the user ends the browsers session, as per MSDN:

When the user closes the browser, the cookie is discarded.

So checking for these on each request will allow the server to know if the user has previously closed the browser.

There are nasty javascript solutions, but these are very much hacks.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • thanks for your answer, I would like avoid javascript solution as for your suggestion. I did not think about cookies approach, it seems a reasonable solution. – GibboK Sep 30 '11 at 09:43
  • Here is [JavaScript solution](http://stackoverflow.com/questions/1824421/detect-browser-close-on-asp-net/1824486#1824486). – Pavel Hodek Dec 08 '11 at 11:50
0

Have a look at the javascript window.OnBeforeUnload handler :

How can I override the OnBeforeUnload dialog and replace it with my own?

This way you can execute some javascript before the user leaves your page.

Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90
0
  • When User visit another URL, directly input in the Browser or clicking another link on the Page

you can do some client side scripting. javascript onbeforeunload Event might help you.

  • When User Log Out from my Web Application.

don't see why this is a problem. this sure is a server side event, isn't it?

  • When a User Close the Browser

that's a problem. what should happen in a case of power failure? or browser crash? or...

Nika G.
  • 2,374
  • 1
  • 17
  • 18
0

You can always run a job in the background using builder.Services.AddHostedService<YourBackgroundService>

William Mendoza
  • 327
  • 2
  • 4