Click here to Skip to main content
Licence CPOL
First Posted 18 Jun 2008
Views 95,730
Bookmarked 52 times

How to Redirect to Another Page when Session Timeout in ASP.NET (when pages contain Ajax UpdatePanel)

By Nguyen Quy Minh | 18 Jun 2008
A small solution to deal with session timeout when using Ajax UpdatePanel
2 votes, 11.1%
1
1 vote, 5.6%
2

3
2 votes, 11.1%
4
13 votes, 72.2%
5
4.57/5 - 18 votes
2 removed
μ 4.07, σa 2.52 [?]

Introduction

Session timeout in ASP.NET is such an important problem with web developer. When session ends, we run into some exceptional situations. There are also many solutions to deal with this problem on the Internet. But few of them have discussed about redirecting to another page when there is session timeout, especially when we work with Ajax UpdatePanel.

So, I suggest a way to do this.

Solution

My main idea is: 3 minutes before session is gone, we will alert the user to save his/her data or make any postbacks. If user still does not do anything, then 5 milliseconds before session goes, we will redirect the page to Login.aspx (or just refresh page by redirecting to itself).

Using the Code

Add the following code to your Master page:

private void CheckSessionTimeout()
{
    string msgSession = 'Warning: Within next 3 minutes, if you do not do anything, '+
               ' our system will redirect to the login page. Please save changed data.';
    //time to remind, 3 minutes before session ends
    int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 3 * 60000; 
    //time to redirect, 5 milliseconds before session ends
    int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 5; 

    string str_Script = @"
            var myTimeReminder, myTimeOut; 
            clearTimeout(myTimeReminder); 
            clearTimeout(myTimeOut); " +
            "var sessionTimeReminder = " + 
		int_MilliSecondsTimeReminder.ToString() + "; " +
            "var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
            "function doReminder(){ alert('" + msgSession + "'); }" +
            "function doRedirect(){ window.location.href="/KB/session/login.aspx"; }" + @"
            myTimeReminder=setTimeout('doReminder()', sessionTimeReminder); 
            myTimeOut=setTimeout('doRedirect()', sessionTimeout); ";

     ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), 
           "CheckSessionOut", str_Script, true);
}
private void Page_Load(object sender, System.EventArgs e)
{
    this.CheckSessionTimeout();
}

If you do not want to redirect to login page, you can just refresh the page by replacing it by this code:

function doRedirect(){ window.location.href=window.location.href; }

If you do not use ScriptManager, you can replace with Page.RegisterClientScriptBlock(...)

Hope this helps.

History

  • 18th June, 2008: Initial post 

License

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

About the Author

Nguyen Quy Minh

Chief Technology Officer

Vietnam Vietnam

Member
MCSD, MSc
http://www.sirvina.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionYour code is simple and works nicely - I adapted to VB.NET and used on the site's Master Page Pinmemberjacklennon6:19 10 Feb '12  
GeneralMy vote of 5 Pinmemberjacklennon6:10 10 Feb '12  
QuestionExcellente article! Thanks PinmemberBruno Lemos7:23 27 Oct '11  
GeneralMy vote of 5 Pinmemberdigjam94221:36 29 Mar '11  
Generallogin.aspx PinmemberAjay Kale New1:17 27 Sep '10  
Generalre automatic redirection to Login.aspx PinmemberAjay Kale New3:34 9 Sep '10  
Questionhow to create secured login page Pinmemberappusri53872:56 4 Nov '09  
QuestionHow to add session renew state to the alert box? PinmemberRobert H.8:59 12 Aug '08  
AnswerRe: How to add session renew state to the alert box? PinmemberNguyen Quy Minh21:28 12 Aug '08  
GeneralRe: How to add session renew state to the alert box? PinmemberRobert H.10:31 13 Aug '08  
GeneralRe: How to add session renew state to the alert box? PinmemberNguyen Quy Minh20:07 13 Aug '08  
GeneralRe: How to add session renew state to the alert box? PinmemberRobert H.6:37 14 Aug '08  
GeneralRe: How to add session renew state to the alert box? Pinmemberrcarroll11:20 14 Jan '09  
GeneralRe: How to add session renew state to the alert box? Pinmemberksalvage4:52 15 Jan '09  
GeneralRe: How to add session renew state to the alert box? PinmemberNguyen Quy Minh18:54 16 Jan '09  
QuestionWorks fine but confirm box comes even when I am working on screen Pinmemberchaggu13:03 9 Jul '08  
AnswerRe: Works fine but confirm box comes even when I am working on screen PinmemberNguyen Quy Minh0:25 10 Jul '08  
QuestionRe: Works fine but confirm box comes even when I am working on screen Pinmemberchaggu4:22 10 Jul '08  
AnswerRe: Works fine but confirm box comes even when I am working on screen PinmemberNguyen Quy Minh7:12 11 Jul '08  
GeneralRe: Works fine but confirm box comes even when I am working on screen Pinmemberchaggu5:19 24 Jul '08  
GeneralMany thanks Pinmembertndang4:14 4 Jul '08  
It works fine and save time for me.
Thanks.
 
tnd

GeneralNo effect when something happened in update panel. PinmemberStressBall1:23 3 Jul '08  
GeneralRe: No effect when something happened in update panel. PinmemberNguyen Quy Minh7:03 3 Jul '08  
GeneralRe: No effect when something happened in update panel. PinmemberStressBall17:49 3 Jul '08  
GeneralRe: No effect when something happened in update panel. PinmemberNguyen Quy Minh4:04 4 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 18 Jun 2008
Article Copyright 2008 by Nguyen Quy Minh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid