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:

  • Replace characters in javascript In server side we have "Replace" method to replace the characters in a string but on javascript replace method will be not useful in certain cases instead of that we can do for loop to replace the particular characters and g… Read More
  • Image Mouse Hover in asp.net To show image in particular div when mouse hover on the image using asp.net   Script <script type="text/javascript" language="javascript">         function ShowToolTip(con) {   &n… Read More
  • Word Documents and Excel Documents in IFRAME In this below example we are converting word OR excel document to HTML. In this case we are converting and giving converted HTML file as ""src"" to ""iframe"". Step By Step Procedure : 1)First step is create one… 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
  • How to Call Method Or Function Or Button From Java Script Call Method Or Function Or Button From Java Script                     Actually we can call button from java script and it will lead the post back also … 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