Click here to Skip to main content
15,889,808 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 695.5K   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

 
Questionmeta http-equiv="refresh" Pin
amrsalah13-Oct-06 3:09
amrsalah13-Oct-06 3:09 
QuestionSession variables vanishing ! Pin
Matthew Hill5-Sep-06 10:46
Matthew Hill5-Sep-06 10:46 
AnswerRe: Session variables vanishing ! Pin
Ach1lles5-Sep-06 23:02
Ach1lles5-Sep-06 23:02 
GeneralRe: Session variables vanishing ! Pin
Matthew Hill6-Sep-06 11:12
Matthew Hill6-Sep-06 11:12 
GeneralRe: Session variables vanishing ! Pin
Ach1lles7-Sep-06 6:32
Ach1lles7-Sep-06 6:32 
GeneralRe: Session variables vanishing ! [modified] Pin
Matthew Hill7-Sep-06 9:48
Matthew Hill7-Sep-06 9:48 
GeneralRe: Session variables vanishing ! Pin
gupta.gourav2126-Oct-09 22:26
gupta.gourav2126-Oct-09 22:26 
GeneralExcellent... Pin
rdardenn24-Aug-06 6:10
rdardenn24-Aug-06 6:10 
QuestionASP Pin
ATC_Jim23-May-06 9:48
ATC_Jim23-May-06 9:48 
AnswerRe: ASP Pin
Ach1lles23-May-06 23:59
Ach1lles23-May-06 23:59 
GeneralRe: ASP Pin
ATC_Jim25-May-06 11:42
ATC_Jim25-May-06 11:42 
GeneralThis was great Pin
Amhed Herrera21-Apr-06 5:14
Amhed Herrera21-Apr-06 5:14 
GeneralVery Nice!! Pin
lexzxzx4-Apr-06 11:07
lexzxzx4-Apr-06 11:07 
GeneralRe: Very Nice!! Pin
Ach1lles4-Apr-06 22:33
Ach1lles4-Apr-06 22:33 
GeneralGood One Pin
shashims15-Mar-06 20:52
professionalshashims15-Mar-06 20:52 
GeneralJust what I needed Pin
Ldanao20-Jan-06 4:30
Ldanao20-Jan-06 4:30 
Generalan alternate way Pin
knnt28-Sep-05 15:47
knnt28-Sep-05 15:47 
GeneralNot always working Pin
Anonymous31-Aug-05 3:00
Anonymous31-Aug-05 3:00 
GeneralRe: Not always working Pin
Member 9623-Oct-06 14:10
Member 9623-Oct-06 14:10 
GeneralVery Fine and working very nicely Pin
Chetan Ranpariya26-Aug-05 19:55
Chetan Ranpariya26-Aug-05 19:55 
GeneralPrevent timeout Pin
Tailor Mehul15-Jun-05 3:33
Tailor Mehul15-Jun-05 3:33 
GeneralProblem Pin
Lumenm1-Apr-05 2:49
Lumenm1-Apr-05 2:49 
GeneralRe: Problem Pin
scott477-Apr-05 13:03
scott477-Apr-05 13:03 
GeneralRe: Problem Pin
Marie131-Aug-06 18:13
Marie131-Aug-06 18:13 
GeneralDisplay Session-Refresh Time on Status Bar Pin
xrow9-Mar-05 23:30
xrow9-Mar-05 23:30 

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.