Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to record the time when a user enters a page and also the time when he leaves this page.

The enter time is recorded by an Sql Command in page_load event.

I have a button for exit. If a user clicks that button and exits, the Sql command is run on button_click Event.

If a user leaves the page he gets an alert message to get back and click the button to record his exit time.

The problem comes when the user ignores even this alert message on page close and exists.

Is there any way i can run an Sql Command when a user leaves the page by clicking on 'X' and leaves the page.

I am using ASP.NET with C#

Thanks for your help.
Posted
Comments
[no name] 19-Dec-15 8:40am    
Why you Need to depend for this on a user Action and not "simply" implement generally "OnClose", something described here: http://stackoverflow.com/questions/16605064/detect-close-browser-in-asp-net[^]

Reliably? No.
What if he turns off the power?
Disconnects his broadband line?
Force closes his browser?
Waits 20 minutes until the session has expired?
Presses the "Back" button?

If this is seriously important to you, then you need to look at adding a timer in Javascript that runs on the client every minute (say) and reports to the server that the page is still open - and record the time then. If you stop getting it, then the page can be assumed to be "closed" and the last recorded time gives the duration.
 
Share this answer
 
Comments
Member 10235977 20-Dec-15 6:03am    
Kindly suggest code or tutorial to do this.
I did the same thing before.

Basically, it is not mission critical to run a web api to log this data for me, so I opted for this solution :

1. I create a cookie for a particular page and log the start time when an user visits the page. (In my case every page has a unique cookie).
2. On any page redirect, I am accessing all other cookie which is not cleaned and log a call to my Web API to register time of all the cookie and also clean them. This way I can ensure that I keep the data safe.
3. OnBeforeUnload event there is a Web API call and cleanup of the cookie such that when an user closes the page and / or clicks on a link, I will try to call a Web API to log the message.
4. If there is a link issue or power issue, my cookie will remain on the browser updated with last time from Timer and the very next time the user visits to the same website, I will update the data to the server.

Remember, it is not a good idea to rely on the data. For instance, if you really require to ensure that your web api is called while leaving the page, this wont be a good idea tough. But it works for me.
 
Share this answer
 
Comments
Member 10235977 20-Dec-15 6:08am    
Kindly suggest code for calling Web API OnBeforeUnload event
Abhishek Sur 20-Dec-15 14:09pm    
Something like this :

$(window).bind('beforeunload', function(){
clearCache();
});

function clearCache() {
$.ajax({
url: "/clearCache/delete",
type: "post",
data:{},
success:function(){
console.log('cache deleted');
}//success
}); //ajax
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900