Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 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();
                }
                else
                {// 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
Updated 7-Jan-11 4:54am
v2
Comments
Sandesh M Patil 7-Jan-11 10:55am    
Edited for better readability

 
Share this answer
 
v2
Comments
Manfred Rudolf 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 Rudolf 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 Rudolf 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.
Cheers!
Manfred Rudolf 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.
I think what you want to look into is the heartbeat pattern

http://ajaxpatterns.org/Heartbeat[^]

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

http://lamahashim.blogspot.com/2009/06/heart-beat-design-pattern-keeping.html[^]
 
Share this answer
 
Comments
Manfred Rudolf 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

http://efreedom.com/Question/1-1431733/Keeping-ASPNET-Session-Open-Alive
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?
I might be a good idea to use jQuery on the browser side:
http://api.jquery.com/jQuery.ajax/[^].

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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900