PC Review


Reply
Thread Tools Rate Thread

AJAX not working when using Session variables.

 
 
=?Utf-8?B?U2hhd24gU2VzbmE=?=
Guest
Posts: n/a
 
      31st May 2007
Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick application working. However, when
attempting to implement the solution, the AJAX calls weren't updating the
screen like the examples were and seemed not to fire until after the long
running process had completed.

I found the only real difference between my application and the quick app is
that I am using Session variables. I added a Session variable to the quick
app and was able to duplicate the behavior.

The quick app I wrote is simple in nature, when the page loads, it begins
making AJAX calls and updates a div on the page with the current time every
second. A button on the page issues a sleep command on the thread for 10
seconds. When not using a Session variable, the time continues to update
after the button is pressed. When using a session variable, the time ceases
to update until the sleep command has expired.

Is there any way to get AJAX to work when using Session variables?
 
Reply With Quote
 
 
 
 
KJ
Guest
Posts: n/a
 
      31st May 2007
Can you please post the relevant code?

"Shawn Sesna" <(E-Mail Removed)> wrote in message
news:9C910EAE-E740-4F9B-A219-(E-Mail Removed)...
> Greetings! I was researching AJAX to provide a solution to displaying
> status
> messages while a long process executed. I found several examples online
> and
> was able to use their code to get a quick application working. However,
> when
> attempting to implement the solution, the AJAX calls weren't updating the
> screen like the examples were and seemed not to fire until after the long
> running process had completed.
>
> I found the only real difference between my application and the quick app
> is
> that I am using Session variables. I added a Session variable to the
> quick
> app and was able to duplicate the behavior.
>
> The quick app I wrote is simple in nature, when the page loads, it begins
> making AJAX calls and updates a div on the page with the current time
> every
> second. A button on the page issues a sleep command on the thread for 10
> seconds. When not using a Session variable, the time continues to update
> after the button is pressed. When using a session variable, the time
> ceases
> to update until the sleep command has expired.
>
> Is there any way to get AJAX to work when using Session variables?



 
Reply With Quote
 
=?Utf-8?B?U2hhd24gU2VzbmE=?=
Guest
Posts: n/a
 
      31st May 2007
Below is all the code you'll need to duplicate the problem.


DEFAULT.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript"
src="javascript/Ajax.js"></script>
<script type="text/javascript" language="javascript">
setTimeout("blah();", 1000);
function clicky() {
blah();
}

function clicky2() {
var a = new Ajax('default.aspx?blah2=a', '', null);
a.returnType = 'JSON';
a.send();
}

function blah() {
var a = new Ajax('default2.aspx?blah=a', '', null);
a.returnType = 'JSON';
a.send();

setTimeout("blah();", 1000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="updater">

</div>
<div id="updater2">

</div>
<!--<input type="button" onclick="clicky();" value="Start One Second
Timer" />-->
<asp:Button id="blahbutton" runat="server" OnClick="blah_click" Text="Do
One-Time 3 Second Call" />
<!--<input type="button" onclick="clicky2();" value="Do One-Time 3
Second Call" />-->
</form>
</body>
</html>

DEFAULT.ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write("POSTED!<br>");
//Session["hi"] = "hi"; <--- UNCOMMENT TO DUPLICATE PROBLEM
}

protected void blah_click(object sender, EventArgs e)
{

System.Threading.Thread.Sleep(10000);
}
}

DEFAULT2.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
Inherits="Default2" %>

DEFAULT2.ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["blah"] != null)
{
Response.Write("var a =
document.getElementById('updater');while(a.childNodes.length >
0){a.removeChild(a.childNodes[0]);}a.appendChild(document.createTextNode('");
if (Application["a"] == null)
Response.Write(DateTime.Now.ToString());
else
Response.Write(Application["a"].ToString());
Response.Write("'));");
Response.End();
}

}
}

AJAX.JS

function Ajax(url, requestXml, callback, sendNow) {
this.url = url;
this.requestMethod = "POST";
this.requestXml = requestXml;
this.callback = callback;
this.async = true;

this.returnType = null;
this.setReturnType = __Ajax_SetReturnType;

this.errorCallback = null;
this.timeoutCallback = null;

this.request = null;

this.send = __Ajax_Send;

if(this.url && this.requestXml && this.callback && sendNow != undefined &&
sendNow)
this.send();
}

Ajax.CreateRequest = __Ajax_CreateRequest;

function __Ajax_CallbackIntercept(instance) {
if(instance.request.readyState == 4) {
if(instance.request.status && instance.request.status == 408 &&
instance.timeoutCallback)
instance.timeoutCallback(instance);
else if(instance.request.status && instance.request.status == 200) {
if(instance.returnType == null && instance.callback)
instance.callback(instance);
else if(typeof(instance.returnType) == "string")
if(instance.returnType == "JSON") {
var json = eval(instance.request.responseText);

if(instance.callback)
instance.callback(json, instance);
} else if(instance.returnType == "TEXT") {
instance.callback(instance.request.responseText, instance);
} else if(instance.callback)
instance.callback(instance.request.responseXML, instance);
else {
if(instance.replaceContents) {
while(instance.idOrDomObjectToInsertInto.childNodes.length > 0)
instance.idOrDomObjectToInsertInto.removeChild(instance.idOrDomObjectToInsertInto.childNodes[0]);
}

instance.idOrDomObjectToInsertInto.appendChild(document.createTextNode(instance.request.responseText));

if(instance.callback)
instance.callback(instance.request.responseText);
}
} else {
if(instance.errorCallback)
instance.errorCallback(instance);
}
}
}

function __Ajax_Send() {
var instance = this;
// alert('hey');
//alert(this.request);
this.request = __Ajax_CreateRequest();
if(this.request.overrideMimeType)
this.request.overrideMimeType("text/xml");
this.request.onreadystatechange = function() {
__Ajax_CallbackIntercept(instance);
}

this.request.open(this.requestMethod, this.url, this.async);
this.request.send(this.requestXml);
}

//returnType values = "JSON", "XML", "TEXT", "HTML", "XHTML"
function __Ajax_SetReturnType(returnType, idOrDomObjectToInsertInto,
replaceContents) {
this.returnType = returnType;
this.idOrDomObjectToInsertInto = idOrDomObjectToInsertInto;
this.replaceContents = replaceContents;

if(typeof(this.idOrDomObjectToInsertInto) == "string")
this.idOrDomObjectToInsertInto =
document.getElementById(idOrDomObjectToInsertInto);
}

function __Ajax_CreateRequest() {
var request = null;

try {
request = new XMLHttpRequest();
} catch(e) {}

if(!request && window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");

return request;
}


"KJ" wrote:

> Can you please post the relevant code?
>
> "Shawn Sesna" <(E-Mail Removed)> wrote in message
> news:9C910EAE-E740-4F9B-A219-(E-Mail Removed)...
> > Greetings! I was researching AJAX to provide a solution to displaying
> > status
> > messages while a long process executed. I found several examples online
> > and
> > was able to use their code to get a quick application working. However,
> > when
> > attempting to implement the solution, the AJAX calls weren't updating the
> > screen like the examples were and seemed not to fire until after the long
> > running process had completed.
> >
> > I found the only real difference between my application and the quick app
> > is
> > that I am using Session variables. I added a Session variable to the
> > quick
> > app and was able to duplicate the behavior.
> >
> > The quick app I wrote is simple in nature, when the page loads, it begins
> > making AJAX calls and updates a div on the page with the current time
> > every
> > second. A button on the page issues a sleep command on the thread for 10
> > seconds. When not using a Session variable, the time continues to update
> > after the button is pressed. When using a session variable, the time
> > ceases
> > to update until the sleep command has expired.
> >
> > Is there any way to get AJAX to work when using Session variables?

>
>
>

 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      31st May 2007
It works but this is likely because you perform concurrent ajax calls. When
a request is processed and session state is enabled, there is a lock so that
the request can access to the session state without having another request
on the same sesion state messing things up...

Do you actually need to read/write session variables ? You could perhaps use
a directive to disable this if not needed. You could also perhaps try to
queue AJAX requests so that you don't have competing AJAX requests at the
same time (or just avoid this design if you can do so)...

---
Patrice

"Shawn Sesna" <(E-Mail Removed)> a écrit dans le message
de news: 9C910EAE-E740-4F9B-A219-(E-Mail Removed)...
> Greetings! I was researching AJAX to provide a solution to displaying
> status
> messages while a long process executed. I found several examples online
> and
> was able to use their code to get a quick application working. However,
> when
> attempting to implement the solution, the AJAX calls weren't updating the
> screen like the examples were and seemed not to fire until after the long
> running process had completed.
>
> I found the only real difference between my application and the quick app
> is
> that I am using Session variables. I added a Session variable to the
> quick
> app and was able to duplicate the behavior.
>
> The quick app I wrote is simple in nature, when the page loads, it begins
> making AJAX calls and updates a div on the page with the current time
> every
> second. A button on the page issues a sleep command on the thread for 10
> seconds. When not using a Session variable, the time continues to update
> after the button is pressed. When using a session variable, the time
> ceases
> to update until the sleep command has expired.
>
> Is there any way to get AJAX to work when using Session variables?



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      31st May 2007
when you use session, asp.net queues the requests to the same session,
to implement session locking, so your analysis of the problem is correct.

just turn off session on the ajax request page (use a web service)

-- bruce (sqlwork.com)



Shawn Sesna wrote:
> Greetings! I was researching AJAX to provide a solution to displaying status
> messages while a long process executed. I found several examples online and
> was able to use their code to get a quick application working. However, when
> attempting to implement the solution, the AJAX calls weren't updating the
> screen like the examples were and seemed not to fire until after the long
> running process had completed.
>
> I found the only real difference between my application and the quick app is
> that I am using Session variables. I added a Session variable to the quick
> app and was able to duplicate the behavior.
>
> The quick app I wrote is simple in nature, when the page loads, it begins
> making AJAX calls and updates a div on the page with the current time every
> second. A button on the page issues a sleep command on the thread for 10
> seconds. When not using a Session variable, the time continues to update
> after the button is pressed. When using a session variable, the time ceases
> to update until the sleep command has expired.
>
> Is there any way to get AJAX to work when using Session variables?

 
Reply With Quote
 
=?Utf-8?B?U2hhd24gU2VzbmE=?=
Guest
Posts: n/a
 
      31st May 2007
Thank you to all that answered, this is very useful information.

"bruce barker" wrote:

> when you use session, asp.net queues the requests to the same session,
> to implement session locking, so your analysis of the problem is correct.
>
> just turn off session on the ajax request page (use a web service)
>
> -- bruce (sqlwork.com)
>
>
>
> Shawn Sesna wrote:
> > Greetings! I was researching AJAX to provide a solution to displaying status
> > messages while a long process executed. I found several examples online and
> > was able to use their code to get a quick application working. However, when
> > attempting to implement the solution, the AJAX calls weren't updating the
> > screen like the examples were and seemed not to fire until after the long
> > running process had completed.
> >
> > I found the only real difference between my application and the quick app is
> > that I am using Session variables. I added a Session variable to the quick
> > app and was able to duplicate the behavior.
> >
> > The quick app I wrote is simple in nature, when the page loads, it begins
> > making AJAX calls and updates a div on the page with the current time every
> > second. A button on the page issues a sleep command on the thread for 10
> > seconds. When not using a Session variable, the time continues to update
> > after the button is pressed. When using a session variable, the time ceases
> > to update until the sleep command has expired.
> >
> > Is there any way to get AJAX to work when using Session variables?

>

 
Reply With Quote
 
=?Utf-8?B?U2hhd24gU2VzbmE=?=
Guest
Posts: n/a
 
      31st May 2007
Web service does indeed work. I added a web service to the application and
all AJAX calls update. Thank you very much for your help

"bruce barker" wrote:

> when you use session, asp.net queues the requests to the same session,
> to implement session locking, so your analysis of the problem is correct.
>
> just turn off session on the ajax request page (use a web service)
>
> -- bruce (sqlwork.com)
>
>
>
> Shawn Sesna wrote:
> > Greetings! I was researching AJAX to provide a solution to displaying status
> > messages while a long process executed. I found several examples online and
> > was able to use their code to get a quick application working. However, when
> > attempting to implement the solution, the AJAX calls weren't updating the
> > screen like the examples were and seemed not to fire until after the long
> > running process had completed.
> >
> > I found the only real difference between my application and the quick app is
> > that I am using Session variables. I added a Session variable to the quick
> > app and was able to duplicate the behavior.
> >
> > The quick app I wrote is simple in nature, when the page loads, it begins
> > making AJAX calls and updates a div on the page with the current time every
> > second. A button on the page issues a sleep command on the thread for 10
> > seconds. When not using a Session variable, the time continues to update
> > after the button is pressed. When using a session variable, the time ceases
> > to update until the sleep command has expired.
> >
> > Is there any way to get AJAX to work when using Session variables?

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session variables not working Andy B. Microsoft ASP .NET 0 26th Dec 2008 06:52 PM
Session variables and static variables loss own contained value. neeraj Microsoft VB .NET 0 6th Sep 2006 01:33 PM
Session variables in Application_error not working tshad Microsoft ASP .NET 8 22nd Feb 2006 09:14 PM
Accessing Session variables when using SQL Server for Session state Michael Microsoft ASP .NET 3 30th Dec 2005 07:56 PM
Re: session variables not working Rick Strahl [MVP] Microsoft ASP .NET 0 28th May 2004 10:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:01 AM.