Click here to Skip to main content
15,881,424 members
Articles / Web Development / HTML
Article

Prevent Session Timeout in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.47/5 (57 votes)
16 Feb 20051 min read 694.1K   144   109
Simple code to prevent a sesison from timing out while a user enters data or edits HTML etc.

Introduction

I have developed ASP and ASP.NET sites for many years and one of the most common end user problems (apart from basic stupidity ;-) is that while the user is entering information into a web form or HTML edit box, the session timeout period will elapse and they lose all the work they have done.

I have tried solutions such as making JavaScript alert the user to click a button or refresh page, but this has restrictions, especially if they are not able to submit the form yet due to required field limitations.

Solution

I recently came across some code which attempted to fix this problem but that was unsuccessful because the author had forgotten the issue of client side caching.

Add to your page the following code:

C#
private void Page_Load(object sender, System.EventArgs e)
{
this.AddKeepAlive();
}
C#
private void AddKeepAlive()
{
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000;
string str_Script = @"
<script type='text/javascript'>
//Number of Reconnects
var count=0;
//Maximum reconnects setting
var max = 5;
function Reconnect(){

count++;
if (count < max)
{
window.status = 'Link to Server Refreshed ' + count.toString()+' time(s)' ;

var img = new Image(1,1);

img.src = 'Reconnect.aspx';

}
}

window.setInterval('Reconnect()',"+ _
    int_MilliSecondsTimeOut.ToString()+ @"); //Set to length required

</script>

";

this.Page.RegisterClientScriptBlock("Reconnect", str_Script);

}

This code will cause the client to request within 30 seconds of the session timeout the page Reconnect.aspx.

The Important Part

Now this works the first time but if the page is cached locally then the request is not made to the server and the session times out again, so what we have to do is specify that the page Reconnect.aspx cannot be cached at the server or client.

This is done by creating the Reconnect.aspx page with this content:

ASP.NET
<%@ OutputCache Location="None" VaryByParam="None" %>
<html>
</html>

The OutputCache directive prevents this from being cached and so the request is made each time. No code behind file will be needed for this page so no @Page directive is needed either.

And that's it.

Hope this helps someone.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Tech Dept
United Kingdom United Kingdom
My Evolution:
TRS-80 Basic, Clipper, C, Better Basic, FORTRAN, C++, Visual Basic, Delphi, C#

Comments and Discussions

 
GeneralPrevent Session Timeout in ASP.NET Pin
sapphire2828-Feb-05 22:18
sapphire2828-Feb-05 22:18 
GeneralElegant solution! Pin
DaNiko27-Feb-05 23:31
DaNiko27-Feb-05 23:31 
GeneralSuggestion Pin
VladCo24-Feb-05 12:56
VladCo24-Feb-05 12:56 
GeneralRe: Suggestion Pin
Ach1lles25-Feb-05 5:15
Ach1lles25-Feb-05 5:15 
GeneralRe: Suggestion Pin
topnotch11-Apr-05 5:39
topnotch11-Apr-05 5:39 
GeneralRe: Suggestion Pin
Ach1lles11-Apr-05 5:48
Ach1lles11-Apr-05 5:48 
GeneralVery good Pin
Thiago Rafael23-Feb-05 2:32
Thiago Rafael23-Feb-05 2:32 
GeneralBetter cache-buster Pin
piers717-Feb-05 0:52
piers717-Feb-05 0:52 
Look, if you really have to do this, a better way to defeat all cache mechanisms (client, server, proxy etc...) is just to add a unique ID on to the end of the querystring - for example the current Date/Time:

img.src="reconnect.aspx?" + escape(new Date().toString());   // really lazy example
</code>

GeneralRe: Better cache-buster Pin
Ach1lles17-Feb-05 2:30
Ach1lles17-Feb-05 2:30 
GeneralWhy Pin
jbkind16-Feb-05 15:02
jbkind16-Feb-05 15:02 
GeneralRe: Why Pin
Anonymously16-Feb-05 15:23
Anonymously16-Feb-05 15:23 
GeneralRe: Why Pin
Ach1lles16-Feb-05 22:29
Ach1lles16-Feb-05 22:29 
GeneralRe: Why Pin
Anonymously17-Feb-05 0:32
Anonymously17-Feb-05 0:32 
GeneralRe: Why Pin
Ach1lles17-Feb-05 2:42
Ach1lles17-Feb-05 2:42 
GeneralRe: Why Pin
jbkind17-Feb-05 7:17
jbkind17-Feb-05 7:17 
GeneralTRS-80? That takes me back... Pin
Keith Farmer16-Feb-05 8:53
Keith Farmer16-Feb-05 8:53 
GeneralRe: TRS-80? That takes me back... Pin
Ach1lles16-Feb-05 22:30
Ach1lles16-Feb-05 22:30 
GeneralRe: TRS-80? That takes me back... Pin
netbard28-Feb-05 4:42
netbard28-Feb-05 4:42 
GeneralRe: TRS-80? That takes me back... Pin
Vladimir Kelman1-Jun-05 21:13
Vladimir Kelman1-Jun-05 21:13 
GeneralRe: TRS-80? That takes me back... Pin
xnolightx22-Mar-05 17:10
xnolightx22-Mar-05 17:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.