Monday, January 11, 2010

web page redirect just before session expired

After session expired it have many was to page redirect. but when we are going to page redirect according to the session variable, it be a problem to detect session variable and page redirect, so that we need to write java script to page load to count time and set page timeout time. then we can detect the time of page ideal time and can be get session values and redirect the page what we want.

before use below function u need to set session timeout time duration within your web application. Default timeout time is 20min in asp.net; in the web config file you can be able to set time duration what you need.

below example is used to page redirect according to session variable in the session and it will execute before the page timeout less than 1 min;

Step 01 :- Set session time out using web config


Step 02 :- Reset time Count at page load and start counter
call check SessionTimeout() function at page Load;

private void CheckSessionTimeout()
{
string msgSession = "Warning: Within next 2 minutes, if you do not do anything, session will be expired.";

int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 2 * 60000;
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 1 * 60000;
string redirectUrl = ResolveUrl("~/BrowserClose.aspx");

string str_Script = @"
var myTimeReminder, myTimeOut;
clearTimeout(myTimeReminder);
clearTimeout(myTimeOut); " +
"var sessionTimeReminder = " +
int_MilliSecondsTimeReminder.ToString() + "; " +
"var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
"function doReminder(){var user; user ='" + Convert.ToString(Session["VaribleName"])+
"'; if (user !='" + string.Empty + "'){ alert('" + msgSession + "');} }" +
"function doRedirect(){var user; user ='" + Convert.ToString(Session["VaribleName"]) +
"'; if (user !='" + string.Empty + "'){alert('Session has expired !');window.location.href='" + redirectUrl + "'; }}" + @"

myTimeOut=setTimeout('doRedirect()', sessionTimeout); ";

//myTimeReminder=setTimeout('doReminder()', sessionTimeReminder); remove alert before session expired;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
"CheckSessionOut", str_Script, true);
}

No comments:

Post a Comment