asp.net ajax 1.0,hello world程序
点击次数:34 次 发布日期:2008-11-26 11:39:32 作者:源代码网
|
源代码网推荐 <1>新建一个asp.net ajax-enabled web site 源代码网推荐 <2>页面布局。Server Controls的标签前缀(Tag Prefix)由atlas变为asp; 源代码网推荐 源代码网推荐 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HelloWorld" %> 源代码网推荐 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 源代码网推荐 <html> 源代码网推荐 <head runat="server"> 源代码网推荐 <title>Hello</title> 源代码网推荐 </head> 源代码网推荐 <body> 源代码网推荐 <form id="form1" runat="server"> 源代码网推荐 <asp:ScriptManager ID="ScriptManager1" runat="server"> 源代码网推荐 <Services> 源代码网推荐 <asp:ServiceReference Path="~/HelloWorldService.asmx" /> 源代码网推荐 </Services> 源代码网推荐 </asp:ScriptManager> 源代码网推荐 <div> 源代码网推荐 你的名字: 源代码网推荐 <input type="text" maxlength="20" id="name" /> 源代码网推荐 <input type="button" id="button1" value="问候" onclick="SayHello()" /> 源代码网推荐 <div id="result"></div> 源代码网推荐 </div> 源代码网推荐 </form> 源代码网推荐 </body> 源代码网推荐 </html><3>客户端脚本。调用服务的方法有些许改变。可以指定默认的回调方法。 源代码网推荐 <script type="text/javascript"> 源代码网推荐 function SayHello() 源代码网推荐 { 源代码网推荐 var fs = HelloWorldService; 源代码网推荐 fs.set_defaultSucceededCallback(OnShow); 源代码网推荐 fs.HelloWorld(document.getElementById("name").value); 源代码网推荐 } 源代码网推荐 function OnShow(result) 源代码网推荐 { 源代码网推荐 var s = document.getElementById("result"); 源代码网推荐 s.innerText = result; 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 源代码网推荐 <4>HelloWorldService服务代码。为了能使服务被asp.net ajax客户端调用,必须给服务指明[ScriptService]属性(为了使用这一属性,需要引用Microsoft.Web.Script.Services命名空间)。 源代码网推荐 1using System; 源代码网推荐 2using System.Web.Services; 源代码网推荐 3using System.Web.Services.Protocols; 源代码网推荐 4using Microsoft.Web.Script.Services; 源代码网推荐 5 源代码网推荐 6[WebService(Namespace = "http://tempuri.org/")] 源代码网推荐 7[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 源代码网推荐 8[ScriptService] 源代码网推荐 9public class HelloWorldService : System.Web.Services.WebService { 源代码网推荐 10 源代码网推荐 11 public HelloWorldService () { 源代码网推荐 12 源代码网推荐 13 //Uncomment the following line if using designed components 源代码网推荐 14 //InitializeComponent(); 源代码网推荐 15 } 源代码网推荐 16 源代码网推荐 17 [WebMethod] 源代码网推荐 18 public string HelloWorld(string name) { 源代码网推荐 19 string hello = String.IsNullOrEmpty(name) ? "无名氏" : name; 源代码网推荐 20 hello += "你好,当前服务器时间是:"; 源代码网推荐 21 hello += DateTime.Now.ToUniversalTime(); 源代码网推荐 22 return hello; 源代码网推荐 23 } 源代码网推荐 24 源代码网推荐 25} 源代码网推荐 http://www.cnblogs.com/sharpaxe/archive/2006/10/24/538395.html 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
