Tue Dec 30, 2008 2:45 am
Tue Dec 30, 2008 7:01 pm
Session.Timeout = 1;
Session["Name"] = "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"] = "Tomas andro";
In the above code I am setting the Session.Timeout to "1" minute. After that I put "Tomas andro" 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"] = "Tomas andro";
}
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 "Tomas andro" 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. ";
public partial class TestPage : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Name"] = "Tomas andro";
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Session["Name"] == null)
throw new ArgumentNullException("Session[Name] is null");
}
}
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);
}
Tue Jan 13, 2009 5:12 am
Mon Aug 17, 2009 2:21 pm
Wed Sep 22, 2010 10:37 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.