Click here to Skip to main content
11,431,829 members (63,014 online)
Click here to Skip to main content

Prevent Session Timeout in ASP.NET

, 16 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;-) 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:

private void Page_Load(object sender, System.EventArgs e)
{
this.AddKeepAlive();
}
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 = '/KB/session/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:

<%@ 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

Share

About the Author

Ach1lles
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

 
GeneralThere is a super simple way to handle this, about 8 lines of code and an iframe Pin
Patrick_SURFThru18-May-11 11:03
memberPatrick_SURFThru18-May-11 11:03 
Generallogin.aspx Pin
Ajay Kale New27-Sep-10 1:16
memberAjay Kale New27-Sep-10 1:16 
Generalsession end Pin
Ajay Kale New9-Sep-10 5:17
memberAjay Kale New9-Sep-10 5:17 
GeneralMy vote of 5 Pin
navneelb3-Aug-10 4:40
membernavneelb3-Aug-10 4:40 
QuestionNot working when run using IIS Pin
shyaz24-Jun-10 12:07
membershyaz24-Jun-10 12:07 
GeneralIt can be done in multiple ways Pin
Eaverae1-Sep-09 23:31
memberEaverae1-Sep-09 23:31 
GeneralGood Article... This is save my day... Pin
yogi230-Apr-09 18:10
memberyogi230-Apr-09 18:10 
Questionstop online banking time out Pin
jsmith0761124-Jan-09 23:14
memberjsmith0761124-Jan-09 23:14 
AnswerRe: stop online banking time out Pin
metweek5-Mar-09 18:28
membermetweek5-Mar-09 18:28 
GeneralASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy28-Dec-08 15:26
memberTim McCurdy28-Dec-08 15:26 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
LongTom891-May-09 6:28
memberLongTom891-May-09 6:28 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy1-May-09 11:57
memberTim McCurdy1-May-09 11:57 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
John Gleeson5-May-09 0:59
memberJohn Gleeson5-May-09 0:59 
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd2-Jun-09 1:12
memberthomasabcd2-Jun-09 1:12 
AnswerRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy2-Jun-09 3:00
memberTim McCurdy2-Jun-09 3:00 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd2-Jun-09 5:13
memberthomasabcd2-Jun-09 5:13 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
koger13-Sep-10 23:58
memberkoger13-Sep-10 23:58 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy14-Sep-10 3:57
memberTim McCurdy14-Sep-10 3:57 
GeneralDisable caching Pin
rtitulaer2-Oct-08 6:33
memberrtitulaer2-Oct-08 6:33 
QuestionWhat about using an ajax call? Pin
Stuart Campbell13-Apr-08 19:54
memberStuart Campbell13-Apr-08 19:54 
AnswerRe: What about using an ajax call? Pin
Jitu9914-Apr-08 7:41
memberJitu9914-Apr-08 7:41 
GeneralRe: What about using an ajax call? Pin
Jorge Bay Gondra19-Sep-08 1:47
memberJorge Bay Gondra19-Sep-08 1:47 
GeneralRe: What about using an ajax call? Pin
Member 307583727-Mar-09 15:02
memberMember 307583727-Mar-09 15:02 
AnswerRe: What about using an ajax call? Pin
johnlysam16-Jul-09 10:52
memberjohnlysam16-Jul-09 10:52 
GeneralPlease Help Pin
shetty7714-Jan-08 5:06
membershetty7714-Jan-08 5:06 
GeneralRe: Please Help Pin
Ach1lles14-Jan-08 5:38
memberAch1lles14-Jan-08 5:38 
GeneralRe: Please Help Pin
shetty7715-Jan-08 5:27
membershetty7715-Jan-08 5:27 
QuestionThanks much, but what is redirect.aspx? Pin
Garry Freemyer30-Nov-07 13:17
memberGarry Freemyer30-Nov-07 13:17 
AnswerIt don't work for me. I'm stuck, They are demanding this! Pin
Garry Freemyer3-Dec-07 14:35
memberGarry Freemyer3-Dec-07 14:35 
GeneralRe: It don't work for me. I'm stuck, They are demanding this! Pin
Garry Freemyer7-Dec-07 12:09
memberGarry Freemyer7-Dec-07 12:09 
GeneralRe: It don't work for me. I'm stuck, They are demanding this! Pin
kannankeril17-Dec-09 12:19
memberkannankeril17-Dec-09 12:19 
QuestionPlease Help Pin
Dubey Yogendra16-Oct-07 5:42
memberDubey Yogendra16-Oct-07 5:42 
AnswerRe: Please Help Pin
Ach1lles18-Oct-07 7:18
memberAch1lles18-Oct-07 7:18 
GeneralThis is an alternative Pin
swedishspeeder2-Sep-07 8:01
memberswedishspeeder2-Sep-07 8:01 
GeneralRe: This is an alternative Pin
fa8011126-May-08 19:19
memberfa8011126-May-08 19:19 
GeneralRe: This is an alternative Pin
rtitulaer2-Oct-08 6:29
memberrtitulaer2-Oct-08 6:29 
GeneralThanks Pin
Captain CAD29-Aug-07 9:51
memberCaptain CAD29-Aug-07 9:51 
Generalyour solution so stupid Pin
Alireza Asgari6-Aug-07 7:58
memberAlireza Asgari6-Aug-07 7:58 
AnswerRe: your solution so stupid Pin
yogi6786-Aug-07 13:40
memberyogi6786-Aug-07 13:40 
GeneralRe: your solution so stupid Pin
Dewey12-Aug-07 12:26
memberDewey12-Aug-07 12:26 
GeneralCool Pin
Hannes Foulds22-Jul-07 22:42
memberHannes Foulds22-Jul-07 22:42 
GeneralThank you! Pin
bpdski20-Jul-07 6:07
memberbpdski20-Jul-07 6:07 
GeneralRe: Thank you! Pin
Jigs Shah14-Dec-08 21:43
memberJigs Shah14-Dec-08 21:43 
GeneralRe: Thank you! Pin
Member 307583727-Mar-09 15:05
memberMember 307583727-Mar-09 15:05 
GeneralPrevent Session Timeout Pin
khanyile12-Jul-07 0:42
memberkhanyile12-Jul-07 0:42 
GeneralRe: Prevent Session Timeout Pin
Ach1lles12-Jul-07 3:07
memberAch1lles12-Jul-07 3:07 
GeneralVery Nice Pin
mBonafe22-May-07 4:55
membermBonafe22-May-07 4:55 
GeneralMany Thanks Pin
Member #4089872-Mar-07 5:03
memberMember #4089872-Mar-07 5:03 
GeneralASP Version Pin
gregsta1-Mar-07 4:54
membergregsta1-Mar-07 4:54 
GeneralRe: ASP Version Pin
rompi8-May-07 21:30
memberrompi8-May-07 21:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 16 Feb 2005
Article Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid