开发者

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

开发者 https://www.devze.com 2023-01-30 06:03 出处:网络
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 nee

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(pageMe开发者_开发技巧thodReturnValue, 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


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.


You could use an anonymous function

PageMethod.SomeMethod(param1,..., function(){onSuccess(foo, importantValue)}, onFailure)
0

精彩评论

暂无评论...
验证码 换一张
取 消