Sunday, 6 May 2012

How to check session timeout from javascript and redirect to login page

JAVASCRIPT :

    var sessionTimeout = <%= Session.Timeout %>

    function DisplaySessionTimeout() {
           sessionTimeout = sessionTimeout - 1;
           window.setTimeout("DisplaySessionTimeout()", 60000);
            if (sessionTimeout == 0) {
                 window.location = "../Login.aspx";
            }
    }
 
Here the  above mentioned script need to be called in the PageLoad of the page which works like a timer. This may be on serverside (or) clientside.

Server side pageload as follows :

protected void Page_Load(object sender, EventArgs e)
{
             Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "DisplaySessionTimeout();",true);                       
}

Client side page load as follows :

function pageLoad()
{
         DisplaySessionTimeout();
}

Description :

<%= Session.Timeout %> will get the time which we have given on "web.config".
Forevery minute it will check the session time and get logged out when session get expired.

Implement this script on "MasterPage" will be useful for entire application which are linked to it.

3 comments:

  1. Hai sir i want the code for photo upload.

    ReplyDelete
    Replies
    1. Which Type Of upload i.e., single file upload or multi file upload ... can u send me Your exact requirement??

      Delete
  2. Wow... This was very helpful... Thank you very much..

    ReplyDelete