Reputation:
How can I send 2 or more ajax requests to the ASP.NET server at the same time, process them on the server and then show the result in some TextBox for each request?
Upvotes: 3
Views: 216
Reputation: 66641
First of all I focus my answer to update panel only, because you use UpdatePanels.
Now, UpdatePanels can not handle 2 request that way, you can not just send one request and before this request completes, to ask for a second one. the main reason is because UpdatePanel send in every Ajax call the ViewState back to the server, and waits for an answer, with that answer the viewstate change to a new one.
So when update panel wait for an answer base on the viewstate A, and gets a replay with viewstate B, its simple not show it. When you ask from the A or the B, UpdatePanel to send/get data, then the viewstate must change to a new viewstate. Now, when you ask a result from A, and before you end, you ask results from B, you going to get 2 different viewstate, and one of them is going to fail because in the middle time have change.
ViewState is not the only think, in every update panel click, the page post back all inputs post to page, both A and B inputs and all the rest inside the page, this is also a problem. Imaging this for example, you trigger A UpdatePanel with inputs A+B, and then before the A ends, you trigger B UpdatePanel with the same inputs. Now A, waits for results from A+B, the same and B, but in the middle time the A+B results from A is now different because B has change them - and fails.
UpdatePanel is very nice out of the box trick for fast and dirty ajax, but this automation have the negative that post all data on the page ! and can handle one request at the time.
Possible solutions is to remove UpdatePanel and use jQuery and Timers on javascript to ask for many calls and wait for many result, and handle the way you show them. Other possible trick is to use iframe, and depends from what you won to show, send and get many request from each iframe.
Upvotes: 1
Reputation: 83358
You can't. The A in Ajax stands for asynchronous. You send your requests one at a time, and when they're done, they'll tell you via callback.
It sounds like you need to define a new web service method—or controller action, if you're using MVC—that does both of these tasks, and call that
Upvotes: 0