How can we pass multiple parameters to onSuccess method of PageMethod?

Janko picture Janko · Dec 9, 2010 · Viewed 13.7k times · Source

I'm calling PageMethod "SameMethod" from javascript method "caller" so that I can get some values from DB. After I get values, control is continuing in "onSuccess" method. Problem is that I need to use some variable values ("importantValue") from javascript method "caller" in "onSuccess" method.

 
function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure)
}

onSuccess method should be something like this:

function onSuccess(pageMethodReturnValue, importantValue ){

}

Is it possible and, if it is, how to pass multiple parameters (besides return values of page method) to "onSuccess" method of PageMethod?

Thanks for help

Answer

Geoff Appleford picture Geoff Appleford · Dec 9, 2010

Pass your importantValue as an additional parameter when calling the PageMethod. (this is usually called the context parameter if you are searching online for more info)

function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure, importantValue)
}

Then you can access the value in the onSuccess callback as follows:

function onSuccess(pageMethodReturnValue, context, methodName){
    // context == 1984
}

Update to explain onSuccess parameters for @JacksonLopes There is a good description on the aspalliance website in an article by Suresh Kumar Goudampally

The important bit (modified to use my parameter names) is:

The success call back method has three parameters:

  • pageMethodReturnValue - Returns the output of the page method.
  • context - This is used to handle different logic when single callback is used for multiple page method requests. We can also pass an array of values as the context parameter.
  • methodName - This parameter returns the name of page method called.