By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,969 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,969 IT Pros & Developers. It's quick & easy.

AJAX not working when using Session variables.

P: n/a
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?
May 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
KJ
Can you please post the relevant code?

"Shawn Sesna" <Sh********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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?

May 31 '07 #2

P: n/a
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.childNo des.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.childNode s.length 0)
instance.idOrDomObjectToInsertInto.removeChild(ins tance.idOrDomObjectToInsertInto.childNodes[0]);
}

instance.idOrDomObjectToInsertInto.appendChild(doc ument.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" <Sh********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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?


May 31 '07 #3

P: n/a
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" <Sh********@discussions.microsoft.coma écrit dans le message
de news: 9C**********************************@microsoft.com...
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?

May 31 '07 #4

P: n/a
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?
May 31 '07 #5

P: n/a
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?
May 31 '07 #6

P: n/a
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?
May 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.