Use ASP. Net ajax to asynchronously call the class methods in Web Services and pages (7): automatic conversion of server and client data types: Generic set type

Source: Internet
Author: User
Tags javascript array

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.4Generic set type

The new generic set type introduced in. NET Framework 2.0 can not only greatly improveProgramThe execution efficiency allows the IDE to learn more types before compilation, and provide more comprehensive auxiliary information during our development process. As a substitute for "traditional" sets, generic set types are increasingly used in various. NET applications.

The ASP. Net Ajax asynchronous communication layer can also automatically generate corresponding client JavaScript types for common generic set types, so that we can easily transmit set data on the client and server.

For type T in the generic set type, if it is a simple type, the ASP. Net Ajax asynchronous communication layer will automatically generate the client JavaScript type for us. For example, the following method returns the list <int> type Web Service:

 
[Webmethod]
 
PublicList <Int> Getgenericintlist ()
 
{
 
List <Int> Intlist =NewList <Int> ();
For(IntI = 0; I <10; ++ I)
 
{
 
Intlist. Add (I * 10 );
 
}
 
 
 
ReturnIntlist;
 
}

If you add the [scriptservice] attribute to the web service and use the scriptmanager control to introduce it to the page, you can directly call the Web service method on the client. The ASP. Net Ajax asynchronous communication layer automatically converts the returned list <int> type to an array in Javascript. Figure 3-22 shows the structure of the returned value in the Visual Studio debugger.

Figure 3-22 structure of the server list <int> type on the client

Consider the following web service method that receives a list <int> parameter:

[Webmethod]
 
Public VoidSendgenericintlist (list <Int> Intlist)
 
{
 
//......
 
}

We can use the following clientCodePass a JavaScript array to the sendgenericintlist () method. Note that the Web service class of the sendgenericintlist () method is lelemanagementservice:

 
VaRIntlist =NewArray ();
 
For(VaRI = 0; I <10; ++ I ){
 
Intlist. Push (I );
 
}
 
Lelemanagementservice. sendgenericintlist (intlist );

ASP. NET Ajax asynchronous communication layer will automatically convert this Javascript array to the list <int> type on the server side. The sendgenericintlist () method will not be noticed during the program running. Figure 3-23 shows the structure of input parameters displayed in the Visual Studio debugger.

Figure 3-23 the client Javascript array can be automatically converted to the server list <int> type

If the type T in the generic set type is a complex type, we need to add the [generatescripttype (typeof ([typename])] attribute for the Web service class, [typename] indicates the name of the complex type. Let's take the preceding employee class as an example. Consider the following to return a list <employee> type web service method:

 
[Webmethod]
 
PublicList <employee> getgenericemployeelist ()
 
{
 
List <employee> employeelist =NewList <employee> ();
 
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;
 
}

After the client calls the proxy of the web service method, Asp. net Ajax asynchronous communication layer automatically converts the returned list <employee> type to an array in Javascript. Figure 3-24 shows the structure of the returned value in the Visual Studio debugger.

Figure 3-24 structure of the server list <employee> type on the client

Consider the following web service method that receives a list <employee> parameter:

 
[Webmethod]
 
Public VoidSendgenericemployeelist (list <employee> employeelist)
 
{
 
//......
 
}

We can use the following client code to pass a JavaScript array containing the client's employee object to the sendgenericemployeelist () method:

 
VaREmployeelist =NewArray ();
 
For(VaRI = 0; I <10; ++ I ){
 
VaREm =NewEmployee ();
 
Em. ID = I;
 
Em. Name ="Name"+ I;
Em. Email ="Name"+ I +"@ Some.com";
 
Em. Salary = 9000;
 
 
 
Employeelist. Push (EM );
 
}
 
Lelemanagementservice. sendgenericemployeelist (employeelist );

ASP. NET Ajax asynchronous communication layer will automatically convert this Javascript array to the list <employee> type on the server side, and the sendgenericemployeelist () method will not be noticed. Figure 3-25 shows the structure of input parameters displayed in the Visual Studio debugger.

Figure 3-25 the Javascript array containing the employee object on the client can be automatically converted to the list <employee> type on the server.

For the dictionary <tkey, tvalue> type, Asp. net Ajax asynchronous communication layer can also automatically convert it to the corresponding type of the client-the premise is that if the tkey or tvalue is of a complex type, we still need to add the [generatescripttype (typeof ([typename])] attribute for the Web service class. [typename] indicates the name of the tkey or tvalue complex type.

Let's take the preceding employee class as an example. Consider the following server-side Web service method:

 
[Webmethod]
PublicDictionary <String, Employee> getgenericemployeedictionary ()
 
{
 
Dictionary <String, Employee> employeedict =NewDictionary <String, Employee> ();
 
For(IntI = 0; I <10; ++ I)
 
{
 
Employee em =NewEmployee (
 
I,
 
String. Format ("Name {0 }", I ),
 
String. Format ("Name {0} @ some.com", I ),
 
5000
);
 
Employeedict [em. Id. tostring ()] = em;
 
}
 
 
 
ReturnEmployeedict;
 
}

If the [scriptservice] attribute is added to the web service and the scriptmanager control is used to introduce it to the page, the dictionary <string, employee> the corresponding client JavaScript Object. The code for the client to call the Web service method is as follows:

 
FunctionPageload (){
 
Peoplemanagementservice. getgenericemployeedictionary (onsucceeded );
 
}

The code of the onsucceeded () callback function is as follows. The returned value of the getgenericemployeedictionary () method is displayed as a table. Note the for (var key in result) Statement in the table, used to traverse the key/value pairs in the client dictionary:

 
FunctionOnsucceeded (result ){
VaRTablebuilder =NewSYS. stringbuilder ("<Table border = 1>");
 
 
 
// Create a table heading row
 
Tablebuilder. append (
 
"<Tr> <TD> id </TD> <TD> name </TD> <TD> email </TD> <TD> salary </TD> </tr>"
 
);
 
 
 
For(VaRKeyInResult ){
 
VaREmployee = Result [Key];
 
// Create a table content row
 
VaRRowstring =
String. Format (
 
"<Tr> <TD >{0} </TD> <TD >{1} </TD> <TD >{2} </TD> <TD >{3} </TD> </tr>",
 
Employee. ID,
 
Employee. Name,
 
Employee. email,
 
Employee. Salary
 
);
 
Tablebuilder. append (rowstring );
 
}
 
 
 
Tablebuilder. append ("</Table>");
 
 
 
$ Get ("Result"). Innerhtml = tablebuilder. tostring ();
 
}

Run this program and we will see the page 3-26.

Figure 3-26 obtain the server-side dictionary <string, employee> type data and display it in a table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.