The page calls JS, and JS accesses the WebService method to implement asynchronous data calling.
Create default. aspx and drag and drop the scriptmanager control to the page.
As follows:
<Asp: scriptmanager runat = "server" id = "script1" enablepagemethods = "true">
<Scripts>
<Asp: scriptreference Path = "~ /JS/JScript. js "/>
</Scripts>
<Services>
<Asp: servicereference Path = "~ /WebService. asmx "/>
</Services>
</ASP: scriptmanager>
<Input type = "button" value = "ajax say hello" id = "btngetajax" onclick = "Return sayhello ()"/>
<Span id = "result"/>
Create a JS file using the following method:
Function sayhello (){
WebService. helloworld (completeajax );
}
Function completeajax (result ){
Document. getelementbyid ("result"). innerhtml = result;
}
Create a new WebService. asmx file, which contains WebService. CS in app_code.
Declare in this class: using system. Web. Services;
Using system. Web. Script. Services;
[Scriptservice]
Public class WebService: system. Web. Services. WebService {
Public WebService (){
// Uncomment the following line if using designed components
// Initializecomponent ();
}
[Webmethod]
Public String helloworld (){
Return "Hello world-li ";
}
}
OK. Run it. This is a very simple example!
I hope I can help you!