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.
The Page_Load code looks like this.
I added two java script functions, one for showing alert message and second one is for hiding that message.
Here are javascript functions
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.
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.