检测ajax提交

I need to run a specific javascript function only when the page is submitted using ajax. I've tried the following:

ScriptManager.RegisterOnSubmitStatement(Page, Page.GetType(), 
    "scriptName", "MyFunction();");

But it seems to run during normal post-backs as well. I've also tried replacing the Page object with the UpdatePanel control but without any difference.

Is it perhaps possible inside the javascript function MyFunction() to detect if it is an ajax or normal submit?

You can utilize Sys.WebForm.PageRequestManager two events

1) add_initializeRequest --> When an ajax post back is initiated

2) add_endRequest --> when an ajax post back is finished.

PageRequestManager also provides a function "get_isInAsyncPostBack()" which tells that if page is still in process of an existing ajax postback.

Example:

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(function () {

 var prm = Sys.WebForms.PageRequestManager.getInstance();

 // Only one async request at a time
 if (prm.get_isInAsyncPostBack()) {
 prm.abortPostBack();
 }

 //Call the function or process you want to perform on ajax request begin
 });

 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
  //Call the fucntion or process you want to perform on ajax request end
 });

Any question, feel free to ask

This code solved the problem. Thanks to @user1341446 for pointing me in the right direction.

Register the javascript function on submit:

ScriptManager.RegisterOnSubmitStatement(Page, Page.GetType(), 
    "scriptName", "MyFunction();");

Inside the function detect if ajax submit:

function MyFunction()
{
  var isAjaxSubmit = false;
  if (typeof (Sys) != "undefined")
    isAjaxSubmit = 
       Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.async;
}