Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Session.Timeout means that after how much time the user's session will expire and the user will not be able to access the items in the Session object. By default the Session.Timeout is 20 minutes. You can change this through the web.config or the page level code. Let's see a small example:

Session.Timeout = 1;

Session["Name"] = "Mohammad Azam";

In the above code I am setting the Session.Timeout to "1" minute. After that I put "Mohammad Azam" in the Session object. This means that if you don't make request to the server and sit idle then after 1 minute you won't be able to access the "Name" key in the Session bag. A good scenario will be a user filling out a long form and you store the values of the form in a Session object. The user fills only couple of fields and then sit idle for 25 minutes keeping in mind that the Session timeout is 20 minutes. Now, if the user tries to retrieve something from the Session object after 25 minutes it will throw the ArgumentNullException as the Session object has been removed.

The question is how do we renew the Session and how do we notify the user that his session is about to expire. The idea is to use JavaScript and notify the user 10 seconds before the Session is about to expire.

First, here is the code from the TestPage.aspx.cs:

 public partial class TestPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["Name"] = "Mohammad Azam";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["Name"] == null)
                throw new ArgumentNullException("Session[Name] is null");
        }
    }

With the click of a button I insert my name "Mohammad Azam" into the Session object. The Session.Timeout is 1 minute and is configured in the web.config file.

Here is the code for the BasePage.cs:

public class BasePage : System.Web.UI.Page
    {

        public BasePage()
        {
            this.Load += new EventHandler(BasePage_Load);
        }

        void BasePage_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(BasePage));
            InjectTimerScript();
        }

        [AjaxPro.AjaxMethod]
        public void UpdateSession()
        {
           
        }

        public void InjectTimerScript()
        {       
            // set the time for 10 seconds less then the expiration time!

            var t = Session.Timeout * 50 * 1000;          
            //var t = 1000;
            string script = String.Empty;

            if (!ClientScript.IsClientScriptBlockRegistered("Timer"))
            {
                script = "function refreshSession() { document.getElementById(\"btnRefreshSession\").style.display ='none'; document.getElementById(\"divTimeOutMessage\").innerHTML = \"\";  DemoWatiN.BasePage.UpdateSession(); } function notifyTimeOut() { document.getElementById(\"btnRefreshSession\").style.display ='block'; document.getElementById(\"divTimeOutMessage\").innerHTML = \" Your Session is about to expire! \"   }  setInterval(\"notifyTimeOut()\"," + t + ")";

                ClientScript.RegisterClientScriptBlock(this.GetType(), "Timer", script, true);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<div id=\"divTimeOutMessage\"></div><input type=\"button\" id=\"btnRefreshSession\" style=\"display:none\" value=\"Refresh Session\" onclick=\"refreshSession()\" />");
            base.Render(writer);
           
        }

When the page loads I register the page to use AJAXPRO.NET library and inject the timer script. The InjectTimerScript method injects the setInterval method which fires 10 seconds before the Session timeout. It also presents the user with a button "Refresh Session". When the "Refresh Session" button is clicked an Ajax call is made to the "UpdateSession" method. There is NO code inside the UpdateSession method. The call/request renews the session for another 1 minute.

This technique helps us to notify the users that their session is about to expire so if they like to continue they must refresh/renew their session.

Posted on Saturday, January 5, 2008 6:21 AM | Back to top


Comments on this post: Session.Timeout and Notification Before Session Expire

# re: Session.Timeout and Notification Before Session Expire
Requesting Gravatar...
i have copied the code & tried runnig the code in asp.net page..it needs ajaxpro assembly.
please help me
Left by raghavendra tilve on Jan 11, 2008 4:13 AM

# re: Session.Timeout and Notification Before Session Expire
Requesting Gravatar...
Google it!


http://www.ajaxpro.info/
Left by Mohammad Azam on Jan 11, 2008 4:17 AM

# re: Session.Timeout and Notification Before Session Expire
Requesting Gravatar...
Sir its nice...cud you help me Sir about this? I used formview to set an appointment then I display it in a gridview i wanted to automatically delete the appointment that i've set in within 1 day....
Left by jassper on Mar 17, 2008 11:29 PM

# re: Session.Timeout and Notification Before Session Expire
Requesting Gravatar...
I want to write A javscript For notifying users about time expirationj of session. Can you Please Help Me?
Left by @run on Jun 22, 2008 8:59 PM

# re: Session.Timeout and Notification Before Session Expire
Requesting Gravatar...
var sessionClock; //min
function startSessionCountDown(timeout){
clearInterval(sessionClock);
sessionClock=setInterval('sessionTimeoutMessage()',timeout*60000);
}

function sessionTimeOutMessage(){
alert('Your session is about to be finished! Please save the form!');
}

And do not forget at the end of the page 23 is time in min:
<script>startSessionCountDown(23);</script>
Left by Jay_69 on Sep 02, 2010 12:53 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net