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.

2 comments:

Unknown said...

sessionAlive.aspx page have to be make under masterpage?

g-link CMS said...

Probably not but the masterpage is always in use as long as the website is open - that goes IF you are using a masterpage. If not you have to find another place to put your iframe.

Is something like
style="visibility:hidden;"
a legal substitute for
height=0 width=0?

On my page height=0 width=0 causes a small black dot i Firefox, the style="visibility:hidden;" does not.

:-)