Thursday, April 22, 2010

How to keep session alive in your site

Some days back I faced a problem of session lose. Today I am writing an article how to keep session alive in asp.net without increasing session time out for the site.
The steps are as follows
First create an aspx Page e.g SessionAlive.aspx.
Then write one line in code behind.

protected void Page_Load(object sender, System.EventArgs e)
{
Response.AddHeader("Refresh", Convert.ToString(( Session.Timeout * 60 ) -120 ));
}

Now in your Master Page add this line
<iframe id=”MaintainSession” src="SessionAlive.aspx" width=0 height=0 runat="server"></ iframe >

Now you can maintain Session as long as the website is open.