Click here to Skip to main content
13,139,254 members (90,887 online)
Rate this:
Please Sign up or sign in to vote.
I'm trying to create a program that keeps a session open if a user is typing in a textbox. Below is the basic ASP.NET code:

<asp:TextBox ID="txtMemoBox" runat="server" OnKeyUp="javascript:ProcessKeyStroke();"></asp:TextBox>
<asp:TextBox ID="hField" runat="server" Text="0"></asp:TextBox>

txtMemoBox is the field that when the user is typing within it we want to keep the session open.

hField will be hidden and is used to count every 20 keystroke within the txtMemoBox field to determine when to renew the session. Therefore if 20 keystrokes haven't been detected within the given session time then the session will automatically expire.

Below is the javascript function ProcessKeyStroke():

function ProcessKeyStroke()
  document.getElementById('<%=hField.ClientID%>').value = parseFloat(document.getElementById('<%=hField.ClientID%>').value) + 1;
            if(document.getElementById('<%=hField.ClientID%>').value > 20)
                document.getElementById('<%=hField.ClientID%>').value = 0;
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                {// code for IE6, IE5
                    xmlhttp = new ActiveXObject();

I understand that XMLHttpRequest() allows calls to and from the server for individual controls. My question is does XMLHttpRequest() and ActiveXObject() alone communicate with the server and therefore renew the session?
Posted 7-Jan-11 4:48am
Updated 7-Jan-11 4:54am
SChristmas 7-Jan-11 10:55am
Edited for better readability
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Manfred R. Bihy 7-Jan-11 12:10pm
[Moved from OP's answer]
Thank you for the reply. I read the article and it doesn't seem to answer my question, it simply states the following statement:
"In most environments, the server-side session will be automatically refreshed when an XMLHttpRequest Call comes in. However, even that's not optimal because the server still doesn't know why browser calls are occurring. For applications using Periodic Refresh, for example, a server-side session will stay alive indefinitely even if the user has left the building."
Manfred R. Bihy 7-Jan-11 12:14pm
[Continued from previous comment]
Maybe you're wanted to provide a different approach. Please advice.

Thank you again.
Manfred R. Bihy 7-Jan-11 12:18pm
@John: Sorry for the the dummy comment. I tried to copy OP's answer to a comment and it didn't take it. I've now split up OP's answer to two comments.
Manfred R. Bihy 7-Jan-11 12:21pm
Good link! 5+
I added that to my Bookmarks. See also Dylans answer with the heartbeat pattern.
MWRivera 9-Feb-11 11:28am
Thank you for the link. I have a partial solution that displays an error message just before the session is about to expire, then as the session is predicted to expire (using a timer) an alternate message is displayed, to let the user know the session may have expired. Instead of the second message do you know of a way I can check if the session is still alive? If it's not alive I will then redirect to the login page. Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I think what you want to look into is the heartbeat pattern[^]

So you'd have some server side code that listens for requests, maybe a webservice called Heartbeat with a method called 'Pulse'

You client side code would periodically call this code to tell the server 'still here!'

NB: You need to implement this carefully. Maybe some script to record 'last key press' time and only call the Pulse method when this is within acceptable range. Otherwise, if you just called it every 30 seconds or whatever, a user could leave the browser open & go on holiday & their session would stay open.

Another example[^]
Manfred R. Bihy 7-Jan-11 12:17pm
Thanks, good link! 5+
MWRivera 10-Jan-11 13:43pm
By referencing other sources are you saying that XMLHttpRequest() and/or ActiveXObject() alone don't renew the session?
Dylan Morley 11-Jan-11 5:15am
No, XMLHttpRequest\ActiveXObject are just technologies available to you to implement AJAX on the client.

You still need to call some server side code. By calling the code, you will include your session cookie in the request (if enabled, I know some people have had issues with IE8)

Here's an example using jQuery and IHttpHandler
MWRivera 14-Jan-11 14:46pm
Thank you for your reply.
Is there a simple AJAX command that I can replace "xmlhttp = new XMLHttpRequest()" with that will renew the session?
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I might be a good idea to use jQuery on the browser side:[^].

A timer feature, to keep the session alive, can be implemented using jQuery Timers[^]

jQuery Data[^] allows you to attach data of any type to DOM element.

Just a few thoughts
Espen Harlinn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 7 Jan 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100