First, create a web project and directly use the default created ASPX page code.
ASPX page:
<% @ 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>
<SCRIPT type = "text/JavaScript">
// The following two methods implement the brushless new handlestatechange of Ajax + WebService. After the callback function is executed, the value returned by helloworld will be assigned to the reuslttext parameter of handlestatechange, which is hard to understand.
Function getstr (){
Jswebservice. helloworld (handlestatechange );
}
function handlestatechange (reuslttext) {
$ get ("result "). innerhtml = reuslttext;
}< br>
Create a WebService
Jswebservice. CSCodeAs follows:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
/// <Summary>
/// Summary of jswebservice
/// </Summary>
[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]
Public class jswebservice: system. Web. Services. WebService {
Public jswebservice (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public String helloworld (){
Return "Hello World ";
}
}
The above is the complete code. Note the following points:
1. The meaning of the two methods in Javascript should be understood, that is, the implementation of refreshing
2. asp: Understand the scriptmanager configuration. The value of the path attribute is the WebService class to be used in Js.
3. The comment of [system. Web. Script. Services. scriptservice] In the WebService class should be canceled. In order to enable js to call this class