Asynchronous calling consists of two parts: asynchronous calling of Web Services and asynchronous calling of Page code
1. asynchronous call of Web Services
Web Service Code
[WebService (Namespace = "http://tempuri.org/
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. ScriptService]
[GenerateScriptType (typeof (CarInfo)]
Public class InnerWs: System. Web. Services. WebService {
Public InnerWs (){
// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
[WebMethod]
Public CarInfo GetCarByPayment (int cost)
{
CarInfo car = new CarInfo ();
If (cost <= 10000) throw new NotSupportedException ("sorry, I can't find a cabbage car with a price lower than 10000 RMB ");
If (cost> 10000 & cost <= 50000) car = new CarInfo ("Chery", "QQ", 22000 );
If (cost> 50000 & cost <= 80000) car = new CarInfo ("Suzuki", "standard", 76400 );
If (cost> 80000 & cost <= 130000) car = new CarInfo ("", "Polo", 110400 );
If (cost> 130000 & cost <= 22000) car = new CarInfo ("", "3000", 191000 );
If (cost> 220000 & Cost <= 340000) Car = new carinfo ("", "Passat", 276200 );
If (cost> 340000 & Cost <= 500000) Car = new carinfo ("BMW", "5I", 389000 );
If (cost> 500000 & Cost <= 700000) Car = new carinfo ("BMW", "7i", 623000 );
If (cost> 700000 & Cost <= 900000) Car = new carinfo ("Benz", "luxury", 888000 );
If (cost> 900000 & Cost <= 1500000) Car = new carinfo ("Porsche", "911gt", 1370000 );
If (cost> 1500000 & cost <= 2500000) car = new CarInfo ("Ferrari", "511", 2200000 );
If (cost> 2500000 & cost <= 3500000) car = new CarInfo ("Rolls-Royce", "classic bulletproof", 3400000 );
If (cost> 3500000) throw new NotSupportedException ("if you have a lot of money, you are advised to find a girlfriend ");
Return car;
}
}
CarInfo class
Public class CarInfo
{
Public CarInfo ()
{}
Public CarInfo (string brand, string type, int price)
{
Brand = brand;
Type = type;
Price = price;
}
Public string Brand {get; set ;}
Public string Type {get; set ;}
Public int Price {get; set ;}
}
Page call code
<Script type = "text/javascript">
Function CompleteCallBack (result, context)
{
Var carObj = new CarInfo ();
CarObj = result;
Sys. debug. traceDump (String. format ("{0}, close to your bid: {1} {2} series, price: $ {3} RMB", context, carObj. brand, carObj. type, carObj. price), "successful ");
}
Function ErrorCallBack (error, context)
{
Sys. Debug. traceDump (context + "," + error. get_message (), "error ");
}
Function Button1_onclick (){
Var cost = $ get ("costBox"). value;
If ($ get ("name"). value = "")
{
Alert ("enter your name ");
Return;
}
If (! Isnan (cost ))
{
If (cost <1 | cost> 9999999)
Alert ("value must be between 1-9999999 ");
Else
{
SYS. Debug. cleartrace ();
Innerws. getcarbypayment (cost, completecallback, errorcallback, $ get ("name"). value );
}
}
Else
Alert ("must be a number ");
}
</Script>
<Style type = "text/css">
# CostBox
{
Width: 188px;
}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services>
<Asp: ServiceReference Path = "~ /InnerWs. asmx "/>
</Services>
</Asp: ScriptManager>
</Div>
How much wocould you pay for your new car?
<Br/>
Name: <input id = "name" type = "text"/> You pay = <input id = "costBox" type = "text"/> <input id = "Button1" type = "button" value = "See what can I buy"
Onclick = "return Button1_onclick ()"/> <br/>
<Br/>
<Fieldset title = "output" style = "width: 608px">
<Legend> output result </legend>
<Textarea id = "TraceConsole" style = "width: 602px; height: 29px ;"
Readonly = "readonly" rows = "1"> </textarea>
</Fieldset>
</Form>
</Body>
</Html>
Running result:
Input: Name = daily, Pay = 345678
Output: Success: every day, the car that is close to your bid is: BMW 5i series; Price: $389000
Input: Name = daily, Pay = 34
Output: Error: Everyday. Sorry, I cannot find a cabbage car with a price less than 10000 RMB.
Ii. asynchronous page call Method
The page method continues to follow the Web service method content, but the signature and attributes are different.
[Webmethod]
[Generatescripttype (typeof (carinfo)]
Public static carinfo getcarbypayment (INT cost)
When calling a page, you must first set scriptmanager
<Asp: scriptmanager id = "scriptmanager1" runat = "server"EnablePageMethods = "true">
</ASP: scriptmanager>
Then, change the method of calling the method
Pagemethods. getcarbypayment (cost, completecallback, errorcallback, $ get ("name"). value );
In addition to the above two points, the page calls of other Web services are the same, and the output results are certainly the same.
Experience
1. The complete signature of the asynchronous callback method and error callback method is
Function methodname (param1, param2,... paramn, callbackfunction, errorfunction, context)
Context is a context parameter and can be of any type.
2. for complex objects returned asynchronously, use the generatescripttype label (you must set the page method to call) to specify the serialization type. The default value is JSON serialization.
3. You can first set the asynchronous Web Service proxy, and set its default methods such as default reconciliation and default errors, and then call the proxy.
For example, VAR proxy = new namespace. classname ();
Proxy. set_defaultSucceededCallback (defaultSucceededCallback );
Proxy. MethodName (param1, param2....) // the default method is called.