This article is from ASP.. Net Ajax programming Chapter II: client-related Microsoft Ajax library Chapter III asynchronous call of Web Services and class methods on pages. For more information, see other articles in this chapter.
3.7.5Array type
The ASP. Net Ajax asynchronous communication layer can automatically generate the corresponding client JavaScript Array for the array type. If the items in the array are of the simple type, no additional operations are required. Consider the following web service method that returns int:
[Webmethod]
Public Int[] Getintarray ()
{
Return New Int[] {1, 2, 3, 4, 5, 6 };
}
After the Web service method is called through the ASP. NET Ajax asynchronous communication layer, the returned value is shown in the structure 3-27 in the Visual Studio debugger.
Figure 3-27 structure of the server-side int [] type on the client
It is also intuitive to pass an array of simple types. Consider the following web service method that accepts an int [] parameter:
[Webmethod]
Public VoidSendintarray (Int[] Intarray)
{
//......
}
We can construct an integer array on the client and pass it to this method through the ASP. NET Ajax asynchronous communication layer:
VaRIntarray = [1, 2, 3, 4, 5];
Lelemanagementservice. sendintarray (intarray );
Figure 3-28 shows the structure of the input integer array in the Visual Studio debugger.
Figure 3-28 the client-side integer Javascript array can be automatically converted to the server-side int [] Type
If the items in the array are complex types, we still need to add the [generatescripttype (typeof ([typename])] attribute for the Web service class, [typename] indicates the name of the complex type. For example, if the preceding employee class is still used, consider the following to return a web service method of the employee [] type. Note that the getgenericemployeelist () method shown in the preceding section is called:
[Webmethod]
PublicEmployee [] getemployeearray ()
{
ReturnGetgenericemployeelist (). toarray ();
}
After the Web service method is called from the client, the returned value is shown in the structure 3-29 in the Visual Studio debugger.
Figure 3-29 structure of the server-side employee [] type on the client
After the [generatescripttype (typeof (employee)] attribute is added to the web service class, it is not hard to understand how to transmit an array of the employee [] type to the server. Consider the following web service method that accepts the employee [] type:
[Webmethod]
Public VoidSendemployeearray (employee [] employeearray)
{
//......
}
Use the following JavascriptCodeConstruct an array containing the client employee object and pass it to the sendemployeearray () method:
VaR employeearray =NewArray ();
For(VAR I = 0; I <10; ++ I ){
Var em =NewEmployee ();
Em. ID = I;
Em. Name ="Name"+ I;
Em. Email ="Name"+ I +"@ Some.com";
Em. Salary = 1000;
Employeearray. Push (EM );
}
Lelemanagementservice. sendemployeearray (employeearray );
The ASP. NET Ajax asynchronous communication layer automatically converts the Javascript array to the server-side employee [] type. Figure 3-30 shows the structure of the input parameters displayed in the Visual Studio debugger.
Figure 3-30 the client's employee array can be automatically converted to the server-side employee [] Type
If necessary, we can also change the array type to a more generalized arraylist. For example, for the following returned arraylist web service method, the client still receives the same employee array as Figure 3-29 because it still contains the employee project.
[Webmethod]
PublicArraylist getemployeelist ()
{
Arraylist employeelist =NewArraylist ();
For(IntI = 0; I <10; ++ I)
{
Employee em =NewEmployee (
I,
String. Format ("Name {0 }", I ),
String. Format ("Name {0} @ some.com", I ),
5000
);
Employeelist. Add (EM );
}
ReturnEmployeelist;
}