Here is a quick & dirty trick to alert users on session timeout. This is not a perfect solution. But it will give reader an idea to work on...
Some facts before we start:
Session doesn't end
- When the user closes his browser
- When the user navigates away from your page
- When user connection lost.
Session ends, when the server hasn't gotten a request from the user in a specific time (Session timeout value).
In this solution, I am using ajaxtoolkit’s modalpopupextender control to alert user about expiring session.
Each time a page is rendered back to the client, I am injecting JavaScript that will show modalpopup two minutes before session timeout. I am passing the session expiry value to the client side java script. This will execute a countdown, and at the end display the Popup.
I added modalpopupextender to the page and set its target control id to a panel. That panel contains alert message and two buttons.
![clip_image002 clip_image002](http://www.vijaykodali.com/Blog/image.axd?picture=clip_image002_thumb.jpg)
The Page_Load code looks like this.
![clip_image004 clip_image004](http://www.vijaykodali.com/Blog/image.axd?picture=clip_image004_thumb_2.jpg)
I added two java script functions, one for showing alert message and second one is for hiding that message.
Here are javascript functions
![clip_image006 clip_image006](http://www.vijaykodali.com/Blog/image.axd?picture=clip_image006_thumb_2.jpg)
That’s it. Run the application and it will check 2 minutes before the timeout and provide user the option to "slide" the session. If user clicks “OK” it will refresh page, which in turn will slide the session. If user clicks “Cancel” the popup will hide.
![clip_image008 clip_image008](https://www.vijaykodali.com/Blog/image.axd?picture=clip_image008_thumb_1.jpg)
You can improve this code on each step. Like for example, to renew session you don’t have to refresh the page. You can just call web service from client side etc.
If you have any questions, leave a comment.