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.

Related Posts:

  • show processing image when click on button and freeze background in asp.net                            This post gives some useful information to beginner's  those don't know to design the page. here i am just … Read More
  • Culture info in RadDateTime Picker In this example we can give culture info for localization in RadDatetimePicker. for this below example u can get different localizations according to their country. it is one of the property in RadDateTimePicke… Read More
  • Different Culture Names CULTURE ||SPEC.CULTURE||ENGLISH NAME af af-ZA Afrikaans af-ZA af-ZA Afrikaans (South Africa) ar ar-SA Arabic ar-AE ar-AE Arabic (U.A.E.) ar-BH ar-BH Arabic … Read More
  • Word document in iframe This is screen shot for iframe.Here In the below Screenshot iframe viewing the word document in the form of html. so i am giving html to the Iframe src. Iframe can view excel,csv,pdf,text file… Read More
  • Convert date format in javascript     Here in this example i am going to explain how to convert date into another format in javascript. Example : Here in this today's date will be obtained in the variable dt in system format and o… Read More

4 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
  3. "For every minute it will check the session time"
    How will that work if you are using setTimeout function?
    I need explanation for this.

    ReplyDelete