Prevent Session Timeout in Asp.net
Prevent Session Timeout in Asp.net [VB]
ASP.Net 2.0 [VB]
Inspired by this article from Code Project. [Thanks Ach1lles ]
I slightly modified few things to work for VB and VS2005 environment. The code works perfect and I’m very happy with it.First I created a Module file and created this function.
Public Function KeepAlive() As String
Dim int_MilliSecondsTimeOut As Integer = (HttpContext.Current.Session.Timeout * 60000) - 30000
Dim sScript As New StringBuilder
sScript.Append("<script type='text/javascript'>" & vbNewLine)
'Number Of Reconnects
sScript.Append("var count=0;" & vbNewLine)
'Maximum reconnects Setting
sScript.Append("var max = 6;" & vbNewLine)
sScript.Append("function Reconnect(){" & vbNewLine)
sScript.Append("count++;" & vbNewLine)
sScript.Append("var d = new Date();" & vbNewLine)
sScript.Append("var curr_hour = d.getHours();" & vbNewLine)
sScript.Append("var curr_min = d.getMinutes();" & vbNewLine)
sScript.Append("if (count < max){" & vbNewLine)
sScript.Append("window.status = 'Refreshed ' + count.toString() + ' time(s) [' + curr_hour + ':' + curr_min + ']';" & vbNewLine)
sScript.Append("var img = new Image(1,1);" & vbNewLine)
sScript.Append("img.src = 'http://localhost/myapp/reconnect.aspx';" & vbNewLine)
sScript.Append("}" & vbNewLine)
sScript.Append("}" & vbNewLine)
sScript.Append("window.setInterval('Reconnect()',")
sScript.Append(int_MilliSecondsTimeOut.ToString() & "); //Set to length required" & vbNewLine)
sScript.Append("</script>")
KeepAlive = sScript.ToString
End Function
All this code does is, build a simple javascript function and its called from server side.The works like this,- Gets the current session timeout duration.
- Subtract 30 seconds from it and assign it to MilliSecondsTimeOut variable
- Create a Reconnect() javascript function
- Create a global variable with max value 6
- Get current hour and Min [just for displaying the last refresh time]
- Verify whether count is less than max value (ie 6)
- If so, change the window status with Text
- Create a dummy image and set reconnect.aspx url as its source [this way a call is made to server and it wont session timeout]Create a timer using window.setInterval and assign the MilliSecondsTimeOut value.
[this way this function is called 30 seconds before session timeout]Tha max value (6) can be set to any number. If its 6 then this session timeout is avoided 5 times.
Default session timeout is 20min. 20 * 5 = 100 mins. Which is good for non-secure page.
Then
On the pages where I want to prevent Session Timeout I entered this line.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try 'dont forget to add this line Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "reconn key", KeepAlive()) If Not Page.IsPostBack Then --- --- --- End If Catch eX as Exception '---- End Try End Sub
Created reconnect.aspx in Visual Studio and deleted the .vb and .designer.vb files.This is the final version of reconnect.aspx
<%@ Page Language="vb" AutoEventWireup="false"%> <%@ OutputCache Location="None" VaryByParam="None" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" mce_href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a href="http://www.w3.org/1999/xhtml" mce_href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" > </html>
Note : When you compile the project sometimes, VS2005 complains reconnect.aspx is not in right format.
So I EXCLUDED this file from my project. Now VS 2005 will compile without complaining and the logic will also work.- Happy Programming

Interesting method of detecting session timeout, one that I’ve never seen before.
kwiksand
November 27, 2007 at 9:23 am
I was wondering if you could write a program for me for time out not to happen.
Could you help me with that. I would pay you and it’s nothing bad (or for wrong purposes).
thanks,
David
David Spencer
December 11, 2007 at 11:19 pm
Hi David,
I don’t think its advisable to make time out not to happen. The server will have overload with endless sessions.
You can increase the session timeout in IIS server, if IIS is not in your control, then you can use code like this and increase the timeout duration.
‘sScript.Append(“var max = 6;” & vbNewLine)
The default timeout is 20 minutes. This code prevents it 5 times which means 5 * 20 = 100 mins.
You can change the number to more than 6. (Its not advisable to keep it active for long duration for the reason said above)
Hope this answer helps you.
gchandra
December 11, 2007 at 11:35 pm
Hi,
I am trying to take a slightly different approach. In my case, I am setting the timeout value to 60 minutes in my web.config file. However, the application is still timing out after 20 minutes. What am I missing?
Thanks,
Scott
Scott
January 22, 2008 at 2:00 pm
Hi Scott.
You have to increase session time out at IIS Level. Just increasing timeout in web.config won’t help.
Thanks
Waqar Ahmad
October 11, 2010 at 1:37 am
hi i have try so manny things but still no hope !!! do you have any thing in c# and increase time out in webconfig.
KM Websol
February 26, 2008 at 6:26 pm
thanks for your help…
KM Websol
February 26, 2008 at 6:27 pm
nice article
http://www.codepal.co.in
Bino
July 11, 2008 at 2:43 am
Nice article. I just wanted to know, why the functionality of session timer refresh is built during page load. Why not the HTML and javascript be directly coded into default.aspx or whatever startup page. Any reply to this query is appreciated.
Anas
August 5, 2008 at 1:41 am
grate code searching for this …….
thanks.
sangram
January 3, 2009 at 4:19 am
Hi,
I need too implement this scenario for particular user of a website. The issue is, if once JavaScript execute after that click on any menu, it gets logout. Any reply to this query is appreciated.
Thanks
Tulika
January 30, 2009 at 7:07 am
Hi,
The fun thing with Javascript is, it will time out after the number of minutes which you have specified decent enough. But internally the ASP.Net is forcing the session to end in 20 minutes. If someone needs the code to session timeout using javascript let me know. Am trying out new ways to block the session timeout issue… May be StateServer.
Mail me at ameenbmirza@gmail.com
Thanks!
Ameen
May 1, 2009 at 9:57 am
After reading through the article, I feel that I really need more info. Could you suggest some resources please?
p.s. Year One is already on the Internet and you can watch it for free.
Watch Year One Online
June 20, 2009 at 2:59 pm
I tried, it does not work for me. I do not get any error message either. I do get the message in the window status bar that it has been refreshed n times….But I do not think there was a postabck after that. There is no way for me to be 100% sure of that because I cannot put a breakpoint in reconnect.aspx. All I know is eventhough the status bar says it has been refreshed my session variables don’t exist anymore.
I appreciate your time in this. Thanks
Shyla
November 6, 2009 at 2:50 pm
OK
S N Suman
August 5, 2010 at 6:18 am