This article describes how to notify users about session expiration and allow them to renew the session.
Controls that need to be added: 1. A Panel with an OK button.
2. A label with session expiration notification message.
3. An AJAX ModalPopupExtender.
4. A dummy button and set ModalPopupExtender’s TargetControlId as this button.
This is how it looks:
<cc1:ModalPopupExtender ID="modext_promptmessage" runat="server" BehaviorID="popup_promptmessage"
PopupControlID="pnl_popup" TargetControlID="btn_dummy">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnl_popup" runat="server" Style="display: none">
<table cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<th>
Prompt Message
</th>
</tr>
<tr>
<td>
<label>Your session is going to expire in 2 minutes. Please click OK to renew it."></label>
<asp:Button ID="btn_ok" runat="server" OnClientClick="ResetSession()" Text="OK" />
</td>
</table>
</asp:Panel>
My code behind class looks like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' checks whether session expired
If Current.Session.Keys.Count = 0 Then
Response.Redirect("sessionexpired.aspx", True)
Exit Sub
End If
' Javascript function PromptSessionExpiration() will be called 2 minutes before session expiration (120000 milliseconds)
Dim strExpireSessionScript As String = String.Format("setTimeout('PromptSessionExpiration()', {0}); ", (Me.Session.Timeout * 60000) - 120000)
Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "expirescript", strExpireSessionScript, True)
End Sub
''' This click event will renew the session, need not add any code inside it
Protected Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
My javascript:
<script language="javascript" type="text/javascript">
function PromptSessionExpiration() {
// the function below will be triggered at the 125th second
setTimeout('CheckSessionStatus()', 125000);
// show popup
$find('popup_promptmessage').show();
}
// handles closing popup
function ResetSession() {
$find('popup_promptmessage').hide();
// this function will be called if user doesn’t respond to the prompt message
// which will redirect to sessionexpired page
function CheckSessionStatus() {
window.location = “sessionexpired.aspx”;
</script>
Thanks.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.