Use ASP. Net ajax to asynchronously call the class methods in Web Services and pages (5): automatic conversion of server and client data types: basic and enumeration types

Source: Internet
Author: User
Tags time and date

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.7Automatic conversion of server and client data types

After reading this, careful readers may have discovered: the previous exampleProgramThe data types passed to the web service and returned from the Web service are simple strings or integers. However, in actual development, it is obviously not enough to pass only data of the string or Integer type. Does the ASP. NET Ajax asynchronous communication layer provide some auxiliary functions for ing between the server-side. Net Type and client-side JavaScript type? That is to say, when ASP. NET Ajax is used for asynchronous communication with the server, can we directly transmit custom types such as datetime, datatable, and even more complex?

The answer is naturally excited-no question! ASP. net Ajax asynchronous communication layer provides powerful server. net and client JavaScript types can be automatically converted. We only need to add configuration, or even no configuration is required, in asynchronous communication, data including basic type, enumeration type, complex type, set (including generic set) type, and array type can be transmitted.

In some special circumstances, if this type of automatic conversion cannot meet the actual needs, we need to have more precise and complete control over a type of conversion rules, so don't worry-with the help of ASP. net Ajax asynchronous communication layer is highly scalable. developers can easily implement their own conversion solutions and "insert" them to existing ASP. net Ajax asynchronous communication layer. The conversion rules for custom server-side and client-side data types are described in section III.

This section describes the automatic conversion between the server-side. Net Type and client JavaScript type provided by the ASP. NET Ajax asynchronous communication layer through the example program.

3.7.1Basic Type

In this section, the basic type refers to the numeric type (including integer and floating point type), string type, boolean type, and time and date type. For these types, the ASP. NET Ajax asynchronous communication layer can automatically convert the server-side. Net Type and client-side JavaScript type without any intervention.

For example, for the following web service definition, a method named sendsimpletypes () is defined, which accepts the six parameters of integer, floating point, String, Boolean, and time and date respectively. The sendsimpletypes () method does not have any practical functions and only serves as a demonstration:

 
[WebService (namespace ="Http://tempuri.org /")]
 
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
 
[Scriptservice]
 
Public ClassSimpletypes: system. Web. Services. WebService
 
{
 
[Webmethod]
Public VoidSendsimpletypes (IntIntvar,FloatFloatvar,
 
StringStringvar,BoolBoolvar, datetime datetimevar)
 
{
 
// Just for testing
 
}
 
}

Then we can create these five types on the client and try to pass them to the Web Service through the proxy generated by ASP. NET Ajax asynchronous communication layer:

 
VaR intvar= 123;
 
VaR floatvar = 123.456;
 
VaR stringvar ="Hello, this is dflying";
VaR boolvar =True;
 
VaR datetimevar =NewDate ();
 
 
 
Simpletypes. sendsimpletypes (
 
Intvar, floatvar, stringvar, boolvar, datetimevar
 
);

Start the debugging function in Visual Studio and add a breakpoint (3-10) to the first line of the web service method sendsimpletypes () defined earlier ).

Figure 3-10 set a breakpoint in the first line of the web service method

When the program runs the sendsimpletypes () method, open the "locals" Window of Visual Studio (CTRL + ALT + V, L) to see sendsimpletypes () the five parameters of the method have been correctly passed to the server. As shown in figure 3-11.

Figure 3-11 view the value of a local variable in the "locals" Window

 

3.7.2Enumeration type

If an enumeration type is used in a Web Service proxy (or the web service method accepts the enumeration type, or the web service method returns the enumeration type), Asp. net Ajax asynchronous communication layer will also automatically generate the client JavaScript version for this enumeration type and convert the client and server types without any intervention.

For example, for the following Web Service and the gettomorrowday () method defined in it, it is used to accept an enumeration of the system. dayofweek type, and then returns an enumeration of the same type, indicating the day of the next day is the day of the week:

 
[WebService (namespace ="Http://tempuri.org /")]
 
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
 
[Scriptservice]
 
Public ClassEnumservice: system. Web. Services. WebService
 
{
 
[Webmethod]
 
PublicDayofweek gettomorrowday (dayofweek today)
 
{
Return(Dayofweek )(((Int) Today + 1) % 7 );
 
}
 
}

If the preceding Web Service proxy named enumservice is introduced to the page:

 
<ASP: scriptmanager ID= "SM" Runat= "Server">
 
<Services>
 
<ASP: servicereference Path= "Services/enumservice. asmx" />
</Services>
 
</ASP: scriptmanager>

Then, write the following client script on the page:

 
FunctionPageload (){
 
Debugger;
 
}

After debugging is started in Visual Studio, the program stops (debugger statement) when the ASP. NET Ajax client framework is initialized (that is, the pageload () method is executed ).

For details about the pageload () method and the initialization process of the ASP. NET Ajax client framework, see Chapter 4th. The methods and skills for debugging debugger statements and ASP. NET Ajax applications are described in the section III.

This is to open a "watch" window (CTRL + ALT + W, and then the number keys 1, 2, 3, or 4), enter "system. dayofweek", and press enter to confirm. Then, click the plus icon on the left of the "system. dayofweek" line to expand the content. The ASP. Net Ajax asynchronous communication layer automatically generates various properties of the client enumeration type for the Server Enumeration type. 3-12, note the seven enumerated values from Sunday to Saturday.

Figure 3-12 ASP. NET Ajax asynchronous communication layer
Client Enumeration type generated by the system. dayofweek Enumeration type on the server side

In this way, the page has the Client Version of the server-side system. dayofweek Enumeration type. Afterwards, we can also pass the system. dayofweek enumeration value of this client directly to the Web Service:

 
Enumservice. gettomorrowday (system. dayofweek. Monday, onsucceeded );

The server will receive "Monday". Of course, this "Monday" is already an enumeration of the server. Net type, as shown in 3-13.

Figure 3-13 the server receives the system. dayofweek enumerated value passed by the client.

Then the callback function of the client receives the Tuesday value ("Tuesday") of the system. dayofweek Enumeration type on the client, as shown in 3-14.

Figure 3-14 the client callback function receives the system. dayofweek enumeration value returned by the server.

For the custom. Net Enumeration type, the ASP. NET Ajax asynchronous communication layer will generate Client versions equally. For example:

 
NamespaceDflying. myenums
 
{
 
Public EnumPlanets
 
{
Mercury,
 
Venus,
 
Earth,
 
Mars,
 
Jupiter,
 
Saturn,
 
Uranus,
 
Neptune
 
}
 
}

If it is used in the Web service method, a Web service method shown below is used to randomly return one of the eight planets:

 
[Webmethod]
 
PublicPlanets getrandomplanet ()
 
{
 
Random r =NewRandom ();
 
Return(Planets )((Int) R. Next (9 ));
 
}

The ASP. NET Ajax asynchronous communication layer generates the corresponding client proxy for it. 3-15 (also in the "watch" Window of Visual Studio ).

Figure 3-15 the ASP. NET Ajax asynchronous communication layer is the client enumeration type generated by the server-side custom Enumeration type

Conclusion: To enable the ASP. Net Ajax asynchronous communication layer to automatically generate the corresponding client enumeration type for the Server Enumeration type, and pass and receive the Enumeration During the call process, we need:

    1. Add the [scriptservice] attribute to the methods that need to be exposed to the client in the Web service or web service;
    2. Add the [webmethod] attribute for the method that the Web Service needs to expose to the client;
    3. A parameter or return value of a method in the Web service class is of this enumeration type;
    4. Add a reference to the Web service in the scriptmanager control on the page;
    5. Use this enumeration type in the following JavaScript syntax on the client:
      [Namespace]. [enumname]. [enumkey]
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.