what I learn is what u c

Prevent Session Timeout in

with 17 comments

Prevent Session Timeout in [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) &#91;' + curr_hour + ':' + curr_min + '&#93;';" & 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(int_MilliSecondsTimeOut.ToString() & "); //Set to length required" & vbNewLine)

        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.


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
  '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="" mce_href=""></a>">
<html xmlns="<a href="" mce_href=""></a>" >

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

Written by gchandra

November 26, 2007 at 3:42 pm

17 Responses

Subscribe to comments with RSS.

  1. Interesting method of detecting session timeout, one that I’ve never seen before.


    November 27, 2007 at 9:23 am

  2. 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).



    David Spencer

    December 11, 2007 at 11:19 pm

  3. 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.


    December 11, 2007 at 11:35 pm

  4. 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?




    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.


      Waqar Ahmad

      October 11, 2010 at 1:37 am

  5. 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

  6. thanks for your help…

    KM Websol

    February 26, 2008 at 6:27 pm

  7. Bino

    July 11, 2008 at 2:43 am

  8. 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.


    August 5, 2008 at 1:41 am

  9. grate code searching for this …….



    January 3, 2009 at 4:19 am

  10. 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.



    January 30, 2009 at 7:07 am

  11. 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



    May 1, 2009 at 9:57 am

  12. 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

  13. 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


    November 6, 2009 at 2:50 pm

  14. OK

    S N Suman

    August 5, 2010 at 6:18 am

  15. Thanks ,very good article

    Aura Cleanmax

    May 2, 2011 at 8:14 am

  16. SURFThru

    May 18, 2011 at 2:59 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: