Processing type
Return complex types
The Ajax Wrapper can not only process integers returned by the serversideadd function. It also supports integers, strings, double, booleans, datetime, datasets, datatables, custom classes, arrays, and other basic types. All other types return their tostring values.
The returned datasets is similar to the real. Net dataset. Assume that a server-side function returns dataset.CodeDisplay the content on the client:
<Script language = "JavaScript">
// Asynchronous call to the mythical "getdataset" server-side function
Function getdataset (){
Ajaxfunctions. getdataset (getdataset_callback );
}
Function getdataset_callback (response ){
VaR DS = response. value;
If (Ds! = NULL & typeof (DS) = "object" & Ds. tables! = NULL ){
VaR S = new array ();
S [S. Length] = "<Table border = 1> ";
For (VAR I = 0; I <Ds. Tables [0]. Rows. length; I ++ ){
S [S. Length] = "<tr> ";
S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. firstname + "</TD> ";
S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. Birthday + "</TD> ";
S [S. Length] = "</tr> ";
}
S [S. Length] = "</table> ";
Tabledisplay. innerhtml = S. Join ("");
}
Else {
Alert ("error. [3001]" + response. Request. responsetext );
}
}
</SCRIPT>
AJAX can also return custom classes. The only requirement is that the serializable attribute must be used. Suppose there are the following classes:
[Serializable ()]
Public class user {
Private int _ userid;
Private string _ firstname;
Private string _ lastname;
Public int userid {
Get {return _ userid ;}
}
Public String firstname {
Get {return _ firstname ;}
}
Public String lastname {
Get {return _ lastname ;}
}
Public user (INT _ userid, string _ firstname, string _ lastname ){
This. _ userid = _ userid;
This. _ firstname = _ firstname;
This. _ lastname = _ lastname;
}
Public user (){}
[Ajaxmethod ()]
Public static user getuser (INT userid ){
// Replace this with a DB hit or something
Return new user (userid, "Michael", "Schwarz ");
}
}
You can register the getuser agent by calling registertypeforajax:
Private void page_load (Object sender, eventargs e ){
Utility. registertypeforajax (typeof (User ));
}
In this way, you can call getuser asynchronously on the client:
<Script language = "JavaScript">
Function getuser (userid ){
User. getuser (getuser_callback );
}
Function getuser_callback (response ){
If (response! = NULL & response. value! = NULL ){
VaR user = response. value;
If (typeof (User) = "object "){
Alert (user. firstname + "" + User. lastname );
}
}
}
Getuser (1 );
</SCRIPT>
The value returned in the response is actually an object that exposes the same attributes (firstname, lastname, and userid) as the server object ).
Http://www.shpan.com/Detail.asp? Id = 398