Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
How To Keep ASP.NET Session Alive
If visitor doesn't make any web request in time interval longer than specified as session timeout (20 minutes by default), session will expire. That means all session variables will be deleted. Sometimes, we want to keep session alive and wait while user is completing some long task.
ASP.NET just remembers time of last request and it doesn't know if visitor is closed browser window or is just doing something else and will return soon. It is certainly worthless to keep session values of user who leaved website. It would be better if we could keep live sessions of visitors who still have page opened.
In this example, RefreshSessionState.aspx page will be called every minute. This is far less than default session timeout which is 20 minutes. If you just want to keep session alive, you can set this time for 19 minutes (19 * 60 * 1000 = 1140000).
Since RefreshSessionState.aspx page is called every minute, you can use ASP.NET server side code for tasks like visitor tracking, how many visitors are currently online, which page each visitor is currently browsing etc.
Keep ASP.NET session alive using jQuery
Code for keeping ASP.NET session alive using jQuery is very short:
Keep ASP.NET session alive using Meta Refresh
One more way to keep ASP.NET session alive is by using Meta Refresh and postback. Of course, we can't refresh complete page because that will annoy visitor, especially if he or she is completing a large form. Instead of that, place small IFRAME tag somewhere on page, and set its src parameter to helper .aspx page. Let's call that page RefreshSessionState.aspx.
HTML code on main page will be:
<iframe height="0" width="0" src="RefreshSessionState.aspx" frameborder="0" />
Code for RefreshSessionState.aspx doesn't require complicated server side code except you want some visitor tracking. Just add meta refresh tag in head section. There are few different methods, I used Response.Write:
<%@ Page Language="C#" %>
I added additional query string "x", to avoid using of cache in some browsers. Query string value will be current time, so URL will always be unique to provide complete postback.
Now, first value in content parameter represents after how much seconds will page refresh. In this example, page will refresh after 900 seconds (15 minutes). It could be any value less than session timeout. Since default session timeout is 20 minutes, this IFRAME will keep session alive while user is working something else.
Keep ASP.NET session alive using ASP.NET Ajax
To keep ASP.NET session alive, one more option is to use ASP.NET Ajax. Timer control is useful in this scenario, since it can send requests in regular time intervals. To see how it works, add to web form one UpdatePanel control and one Timer control. Here is an example markup code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax-Refresh.aspx.cs" Inherits="Ajax_Refresh" %>
Interval property of Timer1 is set to 10000, which is about 10 seconds. You can change this interval according to your needs.
On server side, we'll use Timer_Tick to keep session alive:
[ C# ]
[ VB.NET ]
Partial Class Ajax_Refresh
Refresh methods like these are more scalable than increasing of session timeout. They will keep only wanted sessions, where visitor is not closed a browser and discard others. Of course, you are not obligated to keep sessions on all website pages. Keeping sessions alive could be security problem, because there is an option that user is just forgot to close browser. In this case, it is better if session normally expire than if someone else comes to computer and gets access. You can limit this approach on just few pages where visitor needs more time to complete task.