We can pass parameters to Page methods and then the call
back methods. We can pass the references to the four call back methods to call
when the page method returns. The four call back methods are
·
onSucceed
·
onError
As we discussed we need to have callback function for success
page method call and for unsuccessful page method call. Unsuccessful page
method call means if there is an internal server error on the page.
Using the call back method we can fetch the required result
from the page and use it on the client script as required.
//CallBack method when the page call succeeds
function onSucceed(results, currentContext, methodName)
{
alert(results);
}
//CallBack method when the page call fails due to interna, server error
function onError(results, currentContext, methodName)
{
alert(results);
}
The method names and parameter names are user defined. We
can use any name for the callback methods and their parameters.
The OnSucceed parameter indicates the javascript
function to be called when the call has succeeded and the result parameter has
the result stored in it in our case the string returned from the code
behind file.
The OnError parameter indicates the javascript function to be called
when error occurs. The success call back method has three parameters:
·
Result – Returns the output of the page method.
·
currentContext – This is used to handle different logic when
single callback is used for multiple page method requests. We can also pass
array of values as userContext parameter.
·
methodName – This parameter returns the name of page method
called.
The error call back method has three parameters:
·
Result – Returns the output of the page method.
·
currentContext – This is used to handle different logic when
single callback is used for multiple page method requests. We can also pass
array of values as userContext parameter.
·
methodName – This parameter returns the name of page method
called.
The result here return the type of the error, stacktrace,
message and type of the exceptions. You can display the required information to
the end user using the following methods of error object
get_exceptionType()
get_mesage()
get_stackTrace()
get_statusCode()
get_timedOut()
function onError(result)
{
alert(result.get_message());
}
Sending the userContext parameter
var contextArray = "u1";
PageMethods.GetStatus("p1","p2",onSucceed, onError,contextArray);
And this can be used on the call back to execute different
logic
function onSucceed(result,userContext,methodname)
{
if (userContext = “u1”)
{
alert(result);
}
}