Click here to Skip to main content
13,198,850 members (41,943 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 18 Jun 2008

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

, 18 Jun 2008
Rate this:
Please Sign up or sign in to vote.
A small solution to deal with session timeout when using Ajax UpdatePanel


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.


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(myTimeOut); " +
            "var sessionTimeReminder = " + 
		int_MilliSecondsTimeReminder.ToString() + "; " +
            "var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
            "function doReminder(){ alert('" + msgSession + "'); }" +
            "function doRedirect(){ window.location.href='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)

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.


  • 18th June, 2008: Initial post 


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

You may also be interested in...

Comments and Discussions

GeneralRe: Works fine but confirm box comes even when I am working on screen Pin
chaggu24-Jul-08 4:19
memberchaggu24-Jul-08 4:19 
GeneralMany thanks Pin
tndang4-Jul-08 3:14
membertndang4-Jul-08 3:14 
GeneralNo effect when something happened in update panel. Pin
StressBall3-Jul-08 0:23
memberStressBall3-Jul-08 0:23 
GeneralRe: No effect when something happened in update panel. Pin
Nguyen Quy Minh3-Jul-08 6:03
memberNguyen Quy Minh3-Jul-08 6:03 
GeneralRe: No effect when something happened in update panel. Pin
StressBall3-Jul-08 16:49
memberStressBall3-Jul-08 16:49 
GeneralRe: No effect when something happened in update panel. Pin
Nguyen Quy Minh4-Jul-08 3:04
memberNguyen Quy Minh4-Jul-08 3:04 
GeneralRe: No effect when something happened in update panel. Pin
anukit24-Sep-08 3:48
memberanukit24-Sep-08 3:48 
GeneralSome more improvements... Pin
Tuomas Hietanen27-Jun-08 5:38
memberTuomas Hietanen27-Jun-08 5:38 
If you don't use alert in doReminder()-function, but a hidden html-control (like span) you can include a link in the control to refresh the session (like do a postback, form.submit or load something).

Or if you have a javascript in your input-fields:
After doReminder()-time has passed the input-field javascript could set a flag that user have done some input near session expire time. Then you know that user is near and you can refresh the session just before it expires. A kinda like gmail saving the new message to draft.
GeneralRe: Some more improvements... Pin
Nguyen Quy Minh27-Jun-08 7:22
memberNguyen Quy Minh27-Jun-08 7:22 
Generalthanks! Pin
vegeta4ss25-Jun-08 4:17
membervegeta4ss25-Jun-08 4:17 
QuestionWhat if javascript is disabled? Pin
evolved19-Jun-08 8:04
memberevolved19-Jun-08 8:04 
AnswerRe: What if javascript is disabled? Pin
Nguyen Quy Minh19-Jun-08 21:48
memberNguyen Quy Minh19-Jun-08 21:48 
QuestionWhat about inheritante? Or Forms authentication? Pin
tkrafael_net19-Jun-08 6:04
membertkrafael_net19-Jun-08 6:04 
AnswerRe: What about inheritante? Or Forms authentication? Pin
Nguyen Quy Minh19-Jun-08 7:56
memberNguyen Quy Minh19-Jun-08 7:56 
AnswerRe: What about inheritante? Or Forms authentication? Pin
George Peters2-Jul-08 3:40
memberGeorge Peters2-Jul-08 3:40 
GeneralRe: What about inheritante? Or Forms authentication? Pin
tkrafael_net2-Jul-08 13:27
membertkrafael_net2-Jul-08 13:27 
GeneralRe: What about inheritante? Or Forms authentication? Pin
Nguyen Quy Minh3-Jul-08 6:00
memberNguyen Quy Minh3-Jul-08 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 18 Jun 2008
Article Copyright 2008 by Nguyen Quy Minh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid