I was recently tasked with coming up with a way to keep the session alive in ASP.NET as long as the user had the web page open. That way if they started filling out a form, then went to lunch and came back, they wouldn't lose all of the information just because their session timed out.

The concept is actually pretty simple. You just need to make a call back to the server every once in a while before the session expires to update the session timeout. We figured as long as the user has the browser open on one of our pages they are still working on our site and want their session renewed. Once they leave our site the session can go ahead and expire normally. This would also allow us to reduce the session timeout so we could reclaim session resources on a more frequesnt basis for users that hit the site for a moment then leave.

The primary way I found to accomplish this by asking at the almighty Google was to have a blank image on the page and use javascript to update the image source so that the server is pinged and the session updated. But with ajax why bother? I figured I could do it a lot easier with a simple ajax call that didn't require any image on the page.

What I did was to create an empty aspx that didn't have anything in it, just the default html that Visual Studio generates. Then I created a javascript file that simply uses jquery to do an ajax get request to the empty page. The code is below.

SessionKeepAlive =
{
    delay: 10000,
    url: undefined,
    run: function()
    {
        if (this.url)
        {
            $.get(this.url + "?d=" + escape(new Date().getTime()));
            setTimeout("SessionKeepAlive.run()", this.delay);
        }
    },
    start: function(delay, url)
    {
        // Set ajax timeout to 2 seconds
        $.ajaxSetup({ timeout: 2000 });
       
        // Convert delay to millis
        this.delay = parseInt(delay) * 60000;
        this.url = url;
        setTimeout("SessionKeepAlive.run()", this.delay);
    }
};

You start the keep alive process by calling SessionKeepAlive.start() passing the number of minutes to wait and the url of the page to call. It uses setTimeout() to wait the specified amount of time before calling run(). In run() it does the get request and calls setTimeout() again so it keeps repeating until the user leaves the page. Note that I had to add a parameter to the url to get it to work right in IE. Without adding a unique parameter value to the url, IE was just using the cached results instead of going back to the server. I used the Date.getTime() function to get a unique parameter value.

Since all of my pages extend the same base class I was able to programatically add a link to the javascript file and also register a startup script to call the start() function in page_init.

ClientScript.RegisterClientScriptInclude("keepSessionAlive", "keepSessionAlive.js");
ClientScript.RegisterStartupScript(GetType(), "SessionKeepAlive", "SessionKeepAlive.start(10, 'ping.aspx');", true);

You could also do this in a master page, but I have a number of different master pages and only one page base class, so it was easier to put it in the base class.