Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax with asp.net problem

Status
Not open for further replies.

esdee

Programmer
Apr 12, 2001
143
US
hi
im trying to achieve asynchronous loading of data with asp.net using some ajax tricks. here's a javascript function i use:
Code:
function asyncLoader(url,id1, id2) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  if (x) {
    x.onreadystatechange = function() {
      if (x.readyState == 4 && x.status == 200) {
          onstatechangehandler();
      }
    }
    x.open("GET", url, true);
    x.send(null);
  }
}
i call it several times like this:
Code:
asyncLoader('[URL unfurl="true"]http://localhost/Demo/result1.aspx',id1,[/URL] id2);	
asyncLoader('[URL unfurl="true"]http://localhost/Demo/result2.aspx',id1,[/URL] id2);	
asyncLoader('[URL unfurl="true"]http://localhost/Demo/result3.aspx',id1,[/URL] id2);
and expect that after the requests are sent the responses will be handled in different threads and there will be no waiting - the data will be filled simultaniously, or so i dreamt.
in reality i can see that the requests are fired immediately(asyncroniously indeed), but the server doesnt send me the results from one asp.net page until it has fully processed and sent results from the previous page.
is this what should really happen? or is it IIS? or .. ?

please advise
thanx

------------------------
 
it maybe because ajax requests are asynchronous, but the function that is being called can be called only by one handler at a time therefore giving that effect:
asyncLoader(' id2); - Calls onstatechangehandler()
asyncLoader(' id2); - Calls onstatechangehandler()
asyncLoader(' id2); - Calls onstatechangehandler()

if the requests reply immediately then all 3 will call the function simultaneously. javascript does not have asynchronous functions...

Known is handfull, Unknown is worldfull
 
and do u have an alert by chance???

Known is handfull, Unknown is worldfull
 
the xmlhttp.send function ( ahd thus asyncLoader) return immediately, i've tested it with alert(). then i've set up break points in the respective c# aspx page_Load() method and could see that process doesnt enter it until the previous page_load has finished processing.
thats what surprised me - i thought that IIS would process the aspx pages simultaniously/multithreadedly ?
do you know of another way to accomplish the task?

------------------------
 
>>break points in the respective c# aspx page_Load()

if u do that the execution will stop at the break point, thus making it look as if the load sub is executing one at a time...

Known is handfull, Unknown is worldfull
 
The Page class itself implements the IHttpHandler interface, and thus services requests synchronously."
I do not know enough this say this is the cause. It is from I have gotten ashx to work with the XMLHttpRequest with the help of the above link. Could you please post your onstatechangehandler. I'd like to see how you handle the response[Text | XML].
Marty
 
cappmgr : thank you for the link, it seems promising, i'll read it through and will share my results.
as for the code - im not at my work pc now, but it is nothing complicated, something like:
Code:
divTag.innerHTML = x.responseText
its not even a separate function, just inside the inner anonimous function; if you need more info let me now

vbkris : this doesnt seem right - if you debug multithread code in, say, c++ and stop execution at a break point, the debugger itself will jump in another stopped thread when the scheduler decides it is time to transfer excution to it; so if the pages were processed simultaniously the debugger would jump from one page_load to another.
anyway it is easy to see that each page is excuted one after the other finished by placing dumps, or alerts or whatever.


------------------------
 
esdee,
divTag.innerHTML = x.responseText
This is where I am confused, how did you associate the different responses to the corrrect html element? Any ways Fritz Onion is very sharp and a good read. Good luck.
Marty
 
i do a document.getElementById and pass the desired id as an argument to the function, something like
Code:
divTag = document.getElementById(id1)
divTag.innerHtml = ....

thank you :)

------------------------
 
I have tried the approach in the article posted by cappmgr and also here but it seems that wont do for me:
i rewrote my pages to inherit from IHttpAsyncHandler and then i use an async delegate to do the work, but i stay on the same thread, when in the delegate i make an async httprequest with BeginGetResponse, but the moment i leave the thread i lose the context of the original request and cant write subsequent results to the response stream.

so i guess i'll have to wait for .net 2.0

------------------------
 
esdee,
Please accept my apology. I am wrong about the httprequest. My tests were flawed. I too can not get it to work asynchronously. Still trying though, will post if I get it.
Thank you,
Marty
 
Marty, thank you too,

it looks like we'll have better luck in .net 2.0

until then ... may be there is a way to handle the problem, but my schedule does not allow to investigate further :(

good luck!


Esdee

------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top