I want to execute a query when a user leave a page. Is there any event in asp.net page that happen when a user leave a page? thank you very much
Asked
Active
Viewed 98 times
0
-
After the page is rendered all Page events are complete and the page request is complete so you won't be able to do it this way. What is it you need to trigger? – Stokedout Jan 07 '13 at 19:43
-
The server does not have an avenue to handle this client side event. You need to use javascript to catch the user leaving a page. – Pow-Ian Jan 07 '13 at 19:46
1 Answers
3
No, there is no server event when the user leaves a page, because the user isn't really "in" the page. The server sends a page to the browser, and then the browser displays it, and the server doesn't know what's happening in the browser.
You can catch the onbeforeunload
in the browser, which is triggered when the browser is about to load a new page. Note however that it also fires for a postback, so you may have to keep track of what's happening to determine if the user is really going to a different page.
Note also that the onbeforeunnload
event isn't standard, so it might not be supported in every browser.

Guffa
- 687,336
- 108
- 737
- 1,005
-
Thank you for your answering. So what do you suggest? Because it should work in every browser. Is there any similar way to do that? because i want a list that represent a list of users which is in a special page to admin. – Jan 07 '13 at 19:52
-
1@SaeedTalaee: There is no waterproof solution. You can use the `onbeforeunload` event to handle most users, and for the rest you can keep track of when they last requested the page, and assume that they have left the page when a long enough time has passed. – Guffa Jan 07 '13 at 20:01