5,659,336 members and growing! (20,636 online)
Email Password   helpLost your password?
Web Development » Session State » Sessions and Session State License: The Code Project Open License (CPOL)

How to redirect to another page when session timeout in ASP.NET (when pages contain Ajax UpdatePanel)

By Nguyen Quy Minh

A small solution to deal with session timout when using Ajax UpdatePanel
Javascript, CSS, HTML, XHTML, C#, ASP, ASP.NET, WebForms, Ajax

Posted: 18 Jun 2008
Updated: 18 Jun 2008
Views: 15,911
Bookmarked: 36 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.44 Rating: 3.87 out of 5
2 votes, 14.3%
1
1 vote, 7.1%
2
0 votes, 0.0%
3
2 votes, 14.3%
4
9 votes, 64.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Session timeout in ASP.NET is such an important problem with web developer. When session ended, we run into some exceptional situations. There are also many solutions to deal with this problem on the Internet. But less of them have discussed about redirecting to other page when 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 gone, we will alert for the user for saving their data or make any postbacks. If user still does not do anything yet then 5 miliseconds before session gone, we will redirect the page to Login.aspx (or just refresh page by redirect to itself).

Using the code

Add to your Master page following code:

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 end
    int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 3 * 60000; 
    //time to redirect, 5 miliseconds before session end
    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='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 replace 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 help :)

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


MCP
http://www.sirvina.com
Occupation: Chief Technology Officer
Location: Vietnam Vietnam

Other popular Session State articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
GeneralHow to add session renew state to the alert box?memberRobert H.8:59 12 Aug '08  
GeneralRe: How to add session renew state to the alert box?memberNguyen Quy Minh21:28 12 Aug '08  
GeneralRe: How to add session renew state to the alert box?memberRobert H.10:31 13 Aug '08  
GeneralRe: How to add session renew state to the alert box?memberNguyen Quy Minh20:07 13 Aug '08  
GeneralRe: How to add session renew state to the alert box?memberRobert H.6:37 14 Aug '08  
QuestionWorks fine but confirm box comes even when I am working on screenmemberchaggu13:03 9 Jul '08  
AnswerRe: Works fine but confirm box comes even when I am working on screenmemberNguyen Quy Minh0:25 10 Jul '08  
QuestionRe: Works fine but confirm box comes even when I am working on screenmemberchaggu4:22 10 Jul '08  
AnswerRe: Works fine but confirm box comes even when I am working on screenmemberNguyen Quy Minh7:12 11 Jul '08  
GeneralRe: Works fine but confirm box comes even when I am working on screenmemberchaggu5:19 24 Jul '08  
GeneralMany thanksmembertndang4:14 4 Jul '08  
GeneralNo effect when something happened in update panel.memberStressBall1:23 3 Jul '08  
GeneralRe: No effect when something happened in update panel.memberNguyen Quy Minh7:03 3 Jul '08  
GeneralRe: No effect when something happened in update panel.memberStressBall17:49 3 Jul '08  
GeneralRe: No effect when something happened in update panel.memberNguyen Quy Minh4:04 4 Jul '08  
GeneralRe: No effect when something happened in update panel.memberanukit4:48 24 Sep '08  
GeneralSome more improvements...memberTuomas Hietanen6:38 27 Jun '08  
GeneralRe: Some more improvements...memberNguyen Quy Minh8:22 27 Jun '08  
Generalthanks!membervegeta4ss5:17 25 Jun '08  
GeneralWhat if javascript is disabled?memberevolved9:04 19 Jun '08  
GeneralRe: What if javascript is disabled?memberNguyen Quy Minh22:48 19 Jun '08  
GeneralWhat about inheritante? Or Forms authentication?membertkrafael_net7:04 19 Jun '08  
GeneralRe: What about inheritante? Or Forms authentication?memberNguyen Quy Minh8:56 19 Jun '08  
GeneralRe: What about inheritante? Or Forms authentication?memberGeorge Peters4:40 2 Jul '08  
GeneralRe: What about inheritante? Or Forms authentication?membertkrafael_net14:27 2 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2008
Editor: Chris Maunder
Copyright 2008 by Nguyen Quy Minh
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project