First, you must create a website to create the following files: Web forms (Default. asp tutorial x), JScript File (ajax. js), Web service (SayHelloService. asmx), Class (Hello. cs)
(Add: you need to add a Microsoft. Web. Preview. dll file to download it from CSDN)
- Default. aspx front-end code:
<Supplement> you need to create a new ScriptManager control to partially refresh ajax data.
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Scripts>
<Asp: ScriptReference Assembly = "Microsoft. Web. Preview" Name = "PreviewScript. js"/>
<Asp: ScriptReference Path = "~ /Ajax. js "/>
</Scripts>
<Services>
<Asp: ServiceReference Path = "~ /SayHelloService. asmx "/>
</Services>
</Asp: ScriptManager>
<Div>
<Input id = "btnSayHello" type = "button" value = "SayHello"/>
<Div id = "result"> </div>
</Div>
</Form>
</Body>
</Html>
<Supplement> the role of Preview. dll is here, in order to make the compatriots who are used to the background encoding of the asp.net tutorial feel familiar ....
Var btnSayHello;
Var lblResult;
Sys. Application. add_init (onPageInit );
Function OnFailded (error)
{
LblResult. set_text ("Call failed. Error message: "+ error. get_message ());
} Function OnSucceeded (resultText)
{
LblResult. set_text (resultText );
}
Function btnSayHello_onClick ()
{
SayHelloService. SayHello (OnSucceeded, OnFailded );
} Function onPageInit ()
{
BtnSayHello = new Sys. Preview. UI. Button ($ get ("btnSayHello "));
BtnSayHello. initialize ();
LblResult = new Sys. Preview. UI. Label ($ get ("result "));
LblResult. initialize ();
BtnSayHello. add_click (btnSayHello_onClick );
}
Code of the SayHelloService. cs file:
<Supplement> the function of this Web service class is to construct a "communication platform" between js files and common classes ".
In fact, this can also be done in basic classes. However, to a certain extent, you must add keywords such as [ScriptService] and [WebMethod] to the basic classes to "pollute" the basic classes.
Remember to add keywords such as [ScriptService] and [WebMethod]. Remember to add the [WebMethod] keyword before every time you write a function.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. Script. Services;
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService]
[WebMethod]
Public string SayHello ()
{
Hello myHello = new Hello ();
Return myHello. SayHello ();
}
} Public class SayHelloService: System. Web. Services. WebService {
Public SayHelloService (){
}
Hello code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Public class Hello
{
Public Hello ()
{
}
Public string SayHello ()
{
Return "Hello! Anna ";
}
}