Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript Ajax
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 7-Jan-11 4:48am
MWRivera1.1K
Edited 7-Jan-11 4:54am
v2
Comments
SChristmas at 7-Jan-11 10:55am
   
Edited for better readability
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Does this article help?
 
http://ajaxpatterns.org/archive/Timeout.php[^]

  Permalink  
v2
Comments
Manfred R. Bihy at 7-Jan-11 12:21pm
   
Good link! 5+
I added that to my Bookmarks. See also Dylans answer with the heartbeat pattern.
Manfred R. Bihy at 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 R. Bihy at 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 at 7-Jan-11 12:14pm
   
[Continued from previous comment]
Maybe you're wanted to provide a different approach. Please advice.
 
Thank you again.
MWRivera at 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
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Comments
Manfred R. Bihy at 7-Jan-11 12:17pm
   
Thanks, good link! 5+
MWRivera at 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 at 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 at 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
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 375
1 Prakriti Goyal 242
2 Yogesh Kumar Tyagi 224
3 Sergey Alexandrovich Kryukov 158
4 CPallini 150
0 OriginalGriff 6,742
1 Sergey Alexandrovich Kryukov 5,479
2 Maciej Los 3,474
3 Peter Leow 3,313
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 7 Jan 2011
Copyright © CodeProject, 1999-2014
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