Here we will first provide this Web Service:
Using system;
Using system. collections;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. Web. Script. Services;
/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Scriptservice]
Public class WebService: system. Web. Services. WebService {
Public WebService (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public String helloworld (string instr ){
String STR = "Server ASP. Net Ajax gets the information you entered: "+ instr +" <br/> your IP address is :";
STR + = system. Web. httpcontext. Current. Request. userhostaddress;
STR + = "<br/> current time :";
STR + = system. datetime. Now. tolocaltime ();
Return STR;
}
}
The following is the source file:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> Hello </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
// Call the callback function after the Web Service is successful
Function onshow (result) // The parameter here is the value returned after the Web function is called.
{
VaR S = $ get ("MSG"); // you can also use document. getelementbyid ("MSG") to obtain the HTML control "MSG ";
S. innerhtml = result. tostring ();
}
Function sayhello ()
{
VaR FS = WebService; // the WebService here is a class in the Web Service (do not reverse it with the file name)
FS. helloworld (document. getelementbyid ("testmsg"). Value, onshow); // onshow is the function called after the Web function is called successfully and the returned value is passed to it.
Return false;
}
</SCRIPT>
<Style type = "text/CSS">
# Text1
{
Width: 206px;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "WebService. asmx"/> // call a Web Service
</Services>
</ASP: scriptmanager>
<Div style = "width: 400px; margin-top: 50px; text-align: Center">
<Input id = "testmsg" type = "text" value = "Hello Ajax"/> <br/>
<Asp: button id = "button1" runat = "server" onclientclick = "Return sayhello ()"
TEXT = "Submit to Web Service"/>
<Br/>
</Div>
<Div id = "MSG"> the information is displayed here ..... </Div>
</Form>
</Body>
</Html>