Ali Foroughi
Ali Foroughi

Reputation: 4599

Postback request and ajax request in Asp.net

Is the any way to find out that a request is a normal request (request such as postback operation or redirect or form submit) or an Ajax request?

I am using JQuery lib to send a request and call a Web Method

Upvotes: 0

Views: 842

Answers (1)

Stilgar
Stilgar

Reputation: 23551

Assuming that you are using Web Forms and Update Panels here is what we use:

ScriptManager sm = ScriptManager.GetCurrent(this);
return sm != null && sm.IsInAsyncPostBack;

More general method would be to check for the X-Requested-With header in the request.

Request.Headers["X-Requested-With"] == "XMLHttpRequest"

What is better for you depends on your scenario. Do you want to detect if the page is executing a partial update or you want to know if the browser requested it via XMLHttpRequest?

Another option when using JavaScript to make the AJAX call is to pass a query string parameter indicating that you are making an AJAX call.

Upvotes: 2

Related Questions