Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Any events are available in c# while we close a browser? i know javascript funtion in body unload. But i want in server side?

Thanks in advance.

Regards,
Raj.
Posted
Comments
Ed Nutting 10-May-12 7:49am    
Please look at using AJAX to send a server request (used as the event) from the JavaScript unload event. This is, so far as I'm aware, your only solution.

Hope this helps,
Ed
BobJanova 10-May-12 9:51am    
I'm not sure that any requests you try to fire off in that handler will be given time to go anywhere, even if you're still connected.
Ed Nutting 10-May-12 9:59am    
I'm not sure, I've never tried but given how much else you can do in the unload event (e.g. I think you can still redirect user to another page) I would have thought you'd be able to send off a final request then when your method exits, the page will finally be unloaded. I see two main issues: 1) As you say the request may never get sent and 2) The unload event happens when the user goes to another page (i.e. the page is unloaded) - it is not limited to the browser closing so may not be appropriate. I would like to suggest to the OP that he follows the second suggestion of the solution below i.e. hanlding the normal session end event.

Ed

1 solution

No, as http is stateless, you can only use session timeouts - if you want to be fully standard-conformant. But you might write a little timer activated JS function that performs a short GET to your server, keeping your session alive. If you set the timer to let's say 1s, and your session timeout to 2s, you will be noticed of connection lost in at least 2s. But be aware, that's not browser closure - it's only a connection lost for more than 1s. You can do the same with a Silverlight, Flash or Java applet, or a browser plugin.
But try to imagine all the use-cases of your application, all the communication situations.
Isn't enough to handle the end of a normal session time?
 
Share this answer
 
Comments
Ed Nutting 10-May-12 9:57am    
Right idea but not at all a practical solution - I mean send a request every second? Are you trying to use up as much data as possible? Horrendous coding practice especially if the person is on a mobile... Far better for the OP to redesign their application if this is required. Your second suggestion of handling normal session end is much better. My 3+
Zoltán Zörgő 10-May-12 12:27pm    
The 2s timeout was only for demonstrating. My intention was to direct the questioner to the second proposal. I hope I wasn't missleading him.
Ed Nutting 10-May-12 12:29pm    
That's good then :)

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