学习笔记3-websevice+ajax
感觉websevice如果找到合适的代码上手挺简单的~
无需多言,以下代码说明一切问题(一看就懂的实例~):
Default.aspx
代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head id="Head1" runat="server">
6 <title>Untitled Page</title>
7 <script language="javascript" type="text/javascript">
8 <!--
9
10 function Button1_onclick()
11 {
12
13 ret=WebService.SayHello(document.getElementById("Text1").value, OnComplete, OnTimeOut, OnError);
14 return(true);
15
16 }
17
18 function OnComplete(arg) {
19 alert(arg);
20 }
21
22 function OnTimeOut(arg) {
23 alert("TimeOut encountered when calling Say Hello.");
24 }
25
26 function OnError(arg) {
27 alert("Error encountered when calling Say Hello.");
28 }
29
30
31 // -->
32 </script>
33 </head>
34 <body>
35 <form id="form1" runat="server">
36 <asp:ScriptManager ID="ScriptManager1" runat="server" >
37 <Services>
38 <asp:ServiceReference Path="WebService.asmx" />
39
40 </Services>
41 </asp:ScriptManager>
42
43 <br />
44 <div>
45 <input id="Text1" type="text" /> <br />
46 <br />
47 <input id="Button1" style="width: 200px" type="button" value="SayHello" onclick="return Button1_onclick()" /></div>
48 </form>
49 </body>
50 </html>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head id="Head1" runat="server">
6 <title>Untitled Page</title>
7 <script language="javascript" type="text/javascript">
8 <!--
9
10 function Button1_onclick()
11 {
12
13 ret=WebService.SayHello(document.getElementById("Text1").value, OnComplete, OnTimeOut, OnError);
14 return(true);
15
16 }
17
18 function OnComplete(arg) {
19 alert(arg);
20 }
21
22 function OnTimeOut(arg) {
23 alert("TimeOut encountered when calling Say Hello.");
24 }
25
26 function OnError(arg) {
27 alert("Error encountered when calling Say Hello.");
28 }
29
30
31 // -->
32 </script>
33 </head>
34 <body>
35 <form id="form1" runat="server">
36 <asp:ScriptManager ID="ScriptManager1" runat="server" >
37 <Services>
38 <asp:ServiceReference Path="WebService.asmx" />
39
40 </Services>
41 </asp:ScriptManager>
42
43 <br />
44 <div>
45 <input id="Text1" type="text" /> <br />
46 <br />
47 <input id="Button1" style="width: 200px" type="button" value="SayHello" onclick="return Button1_onclick()" /></div>
48 </form>
49 </body>
50 </html>
WebService.cs
代码
1 using System;
2 using System.Collections;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Services;
6 using System.Web.Services.Protocols;
7 using System.Xml.Linq;
8
9 /// <summary>
10 ///WebService 的摘要说明
11 /// </summary>
12 [WebService(Namespace = "http://tempuri.org/")]
13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
15 [System.Web.Script.Services.ScriptService]
16 public class WebService : System.Web.Services.WebService {
17
18 public WebService () {
19
20 //如果使用设计的组件,请取消注释以下行
21 // InitializeComponent();
22 }
23
24 [WebMethod]
25 public string SayHello(string Name)
26 {
27 return "Hello: "+Name;
28 }
29 }
2 using System.Collections;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Services;
6 using System.Web.Services.Protocols;
7 using System.Xml.Linq;
8
9 /// <summary>
10 ///WebService 的摘要说明
11 /// </summary>
12 [WebService(Namespace = "http://tempuri.org/")]
13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
15 [System.Web.Script.Services.ScriptService]
16 public class WebService : System.Web.Services.WebService {
17
18 public WebService () {
19
20 //如果使用设计的组件,请取消注释以下行
21 // InitializeComponent();
22 }
23
24 [WebMethod]
25 public string SayHello(string Name)
26 {
27 return "Hello: "+Name;
28 }
29 }