Use ASP. Net ajax to asynchronously call the class methods of Web Services and pages (3): Keep the user Context

Source: Internet
Author: User

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.4Keep user Context

Asynchronous execution means that the functions originally executed from start to end are intercepted and divided into two different functions. The relationship between these two functions is often very close. For example, sometimes we need to access the variables computed in the previous function in the next function (that is, the callback function, or get the context and other information when the previous function is executed in the next function. In particular, if several different asynchronous functions provide the same callback function for processing, we generally need to know which function triggers this callback in the callback function. To meet these needs,ProgramThis naturally introduces the concept of user context.

The asynchronous call model provided by ASP. NET Ajax asynchronous communication layer also provides support for passing user context information. We also use a simple example program to demonstrate its implementation.

The functions of the program are not complex: both buttons call the same web service on the server asynchronously and specify the same callback function. After obtaining the current time of the server, the function is displayed, at the same time, the information of the button clicked by the user is displayed. See Figure 3-7 and Figure 3-8. Note that the user context is different.

Figure 3-7 click the first button to obtain the current server time

Figure 3-8 click the second button to obtain the current server time

The Web service statement is as follows, and the current server time is returned:

 
[WebService (namespace ="Http://tempuri.org /")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
 
[Scriptservice]
 
Public ClassUsercontextservice: system. Web. Services. WebService
 
{
 
 
 
[Webmethod]
 
Public StringGetserverdatetime ()
 
{
 
ReturnDatetime. Now. tostring ();
 
}
 
 
 
}

The scriptmanager control on the page references the Web service to generate its client call Proxy:

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

The interface elements are not complex: The first two buttons call the Web service defined above, and the <Div/> with the ID of result is used to display the call result:

<Input ID= "Btngetserverdatetime1" Type= "Button" Value= "Get serverdate time 1"
 
Onclick= "Return btngetserverdatetimeeffeconclick ()" />
 
<Input ID= "Btngetserverdatetime2" Type= "Button" Value= "Get serverdate time 2" 
 
Onclick= "Return btngetserverdatetime2_onclick ()" />
<Div ID= "Result"> </Div>

The processing functions of the click events of the two buttons are as follows:

 
FunctionBtngetserverdatetimeeffeconclick (){
 
Usercontextservice. getserverdatetime (onsucceeded, onfailed,"Button 1");
 
}
 
 
 
FunctionBtngetserverdatetime2_onclick (){
 
Usercontextservice. getserverdatetime (onsucceeded, onfailed,"Button 2");
 
}

Note that the bold part -- well, it seems that the syntax for the client to call the Web Service Proxy just summarized above: [namespace]. [classname]. [methodname] (param1, param2 ..., Onsucceeded, onfailed) has a new change: a parameter is added after the successful callback function and the failed callback function. This is the user context parameter to be demonstrated in this example, here we set them to "button 1" and "button 2" respectively ".

In this way, the syntax for the client to call the Web Service proxy is changed:

 
[Namespace]. [classname]. [methodname] (param1, param2 ..., Onsucceeded, onfailed, usercontext)

Correspondingly, the signature of the onsucceeded () callback function also changes. A parameter representing the user context object is added after the method return value parameter. In this example, the context parameters ("button 1" and "button 2") are displayed.CodeIn bold Section:

 
Function onsucceeded (result, context ){
 
$ Get ("Result"). Innerhtml =
 
"<Strong> current server datetime: </strong>"+ Result
 
+"<Br/> <strong> User context: </strong>"+ Context;
}

This completes the sample program. Note that the user context can pass not only strings, but also complex JavaScript objects, just like in the example program. For example:

 
VaRContextobj = {
 
Name:"Dflying",
 
Age: 88,
 
Dateofbirth:NewDate ()
 
};
 
Somenamespace. someclass. somemethod (param1, param2, onsucceeded,
 
Onfailed, contextobj );

Then in the onsucceeded () callback function, we can access the passed user context object:

 
FunctionOnsucceeded (result, context ){
 
VaRName = context. Name;
 
VaRAge = context. Age;
VaRDateofbirth = context. dateofbirth;
 
}
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.