Click here to Skip to main content
12,063,445 members (75,309 online)
Click here to Skip to main content
Add your own
alternative version

Stats

159.4K views
63 bookmarked
Posted

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

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

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)

Share

About the Author

Nguyen Quy Minh
Chief Technology Officer
Vietnam Vietnam
MCSD, MSc
http://www.sirvina.com

You may also be interested in...

Comments and Discussions

 
QuestionGreat solution, you are genius Nguyen Pin
Member 26560432-May-14 8:33
memberMember 26560432-May-14 8:33 
Questionsession Pin
aashish tyagi2-Apr-14 21:47
memberaashish tyagi2-Apr-14 21:47 
QuestionHelp Pin
charmax12-Dec-12 12:03
membercharmax12-Dec-12 12:03 
GeneralMy vote of 5 Pin
ChandraShekar.SK7-Aug-12 5:41
memberChandraShekar.SK7-Aug-12 5:41 
QuestionYour code is simple and works nicely - I adapted to VB.NET and used on the site's Master Page Pin
jacklennon10-Feb-12 6:19
memberjacklennon10-Feb-12 6:19 
I have seen so many articles in the about redirecting to another page after timeout.
In my opinion this article is the better, for its simplicity.
I adapted the code to VB.NET and inserted the procedure in the web site master page code behind.
    Private Sub CheckSessionTimeout()
        Dim msgSession As String = _
        "Warning: in 3 minutes, if you don't do anything " + _
        " our system will redirect you to the login page. Please, save all your unsaved data."
        ' time to remind, 3 minutes before session ends
        Dim int_MilliSecondsTimeReminder As Long = (Me.Session.Timeout * 60000) - 3 * 60000
        ' time to redirect, 5 milliseconds before session ends
        Dim int_MilliSecondsTimeOut As Long = (Me.Session.Timeout * 60000) - 5
        Dim str_Script As String = _
            "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=""sessionexpired.aspx""; }" + _
            "myTimeReminder=setTimeout('doReminder()', sessionTimeReminder);" + _
            "myTimeOut=setTimeout('doRedirect()', sessionTimeout); "
        ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "CheckSessionOut", str_Script, True)
    End Sub
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    CheckSessionTimeout()
End Sub
Jacques Zetune

GeneralMy vote of 5 Pin
jacklennon10-Feb-12 6:10
memberjacklennon10-Feb-12 6:10 
QuestionExcellente article! Thanks Pin
Bruno Lemos27-Oct-11 7:23
memberBruno Lemos27-Oct-11 7:23 
GeneralMy vote of 5 Pin
digjam94229-Mar-11 21:36
memberdigjam94229-Mar-11 21:36 
Generallogin.aspx Pin
Ajay Kale New27-Sep-10 1:17
memberAjay Kale New27-Sep-10 1:17 
Generalre automatic redirection to Login.aspx Pin
Ajay Kale New9-Sep-10 3:34
memberAjay Kale New9-Sep-10 3:34 

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.

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