当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET AJAX Beta2 调用本地WebService的一些改变

 ASP.NET AJAX Beta2 调用本地WebService的一些改变

点击次数:23 次 发布日期:2008-11-26 11:01:59 作者:源代码网
源代码网推荐     
源代码网推荐  发现最新版本的改动很大,下面就测试情况作一下说明(这里借用官方的例子):
源代码网推荐  
源代码网推荐  1、首先建一个WebService 文件(HelloWorldService.asmx),代码如下:
源代码网推荐  
源代码网推荐  <%@ WebService Language="C#" Class="Samples.AspNet.HelloWorldService" %>
源代码网推荐  
源代码网推荐  using System;
源代码网推荐  using System.Web;
源代码网推荐  using System.Web.Services;
源代码网推荐  using System.Xml;
源代码网推荐  using System.Web.Services.Protocols;
源代码网推荐  using Microsoft.Web.Script.Services;
源代码网推荐  
源代码网推荐  namespace Samples.AspNet
源代码网推荐  {
源代码网推荐  
源代码网推荐   [WebService(Namespace = "http://tempuri.org/")]
源代码网推荐   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
源代码网推荐   [ScriptService]
源代码网推荐   public class HelloWorldService : System.Web.Services.WebService
源代码网推荐   {
源代码网推荐  
源代码网推荐   [WebMethod]
源代码网推荐   public string HelloWorld(String query)
源代码网推荐   {
源代码网推荐   string inputString = Server.HtmlEncode(query);
源代码网推荐   if (!String.IsNullOrEmpty(inputString))
源代码网推荐   {
源代码网推荐   return String.Format("Hello, you queried for {0}. The "
源代码网推荐   + "current time is {1}", inputString, DateTime.Now);
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   return "The query string was null or empty";
源代码网推荐   }
源代码网推荐   }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  这里要说明的是[ScriptService] 属性,只有加上这个性属性,才能在页面中通过js进行异步调用;
源代码网推荐  
源代码网推荐  2、建一个调用页面(AjaxScript1.aspx),如下:
源代码网推荐  <%@ Page Language="C#" %>
源代码网推荐  <!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 id="Head1" runat="server">
源代码网推荐   <title="测试一" />
源代码网推荐   <style type="text/CSS">
源代码网推荐   body { font: 11pt Trebuchet MS;
源代码网推荐   font-color: #000000;
源代码网推荐   padding-top: 72px;
源代码网推荐   text-align: center }
源代码网推荐  
源代码网推荐   .text { font: 8pt Trebuchet MS }
源代码网推荐   </style>
源代码网推荐  
源代码网推荐   </head>
源代码网推荐   <body>
源代码网推荐   <form id="Form1" runat="server">
源代码网推荐   <asp:ScriptManager runat="server" ID="scriptManager">
源代码网推荐   <Services>
源代码网推荐   <asp:ServiceReference path="~/HelloWorldService.asmx" />
源代码网推荐   </Services>
源代码网推荐   </asp:ScriptManager>
源代码网推荐  
源代码网推荐   <div>
源代码网推荐   Search for
源代码网推荐   <input id="SearchKey" type="text" />
源代码网推荐   <input id="SearchButton" type="button" value="Search"
源代码网推荐   onclick="DoSearch()" />
源代码网推荐   </div>
源代码网推荐   </form>
源代码网推荐   <hr style="width: 300px" />
源代码网推荐   <div>
源代码网推荐   <span id="Results"></span>
源代码网推荐   </div>
源代码网推荐   <script type="text/javascript">
源代码网推荐  
源代码网推荐   function DoSearch()
源代码网推荐   {
源代码网推荐   var SrchElem = document.getElementById("SearchKey");
源代码网推荐   Samples.AspNet.HelloWorldService.HelloWorld(SrchElem.value, OnRequestComplete);
源代码网推荐   }
源代码网推荐  
源代码网推荐   function OnRequestComplete(result)
源代码网推荐   {
源代码网推荐   var RsltElem = document.getElementById("Results");
源代码网推荐   RsltElem.innerHTML = result;
源代码网推荐   }
源代码网推荐  
源代码网推荐   </script>
源代码网推荐   </body>
源代码网推荐  </html>
源代码网推荐  
源代码网推荐  注意,这里的<asp:ScriptManager runat="server" ID="scriptManager">
源代码网推荐   <Services>
源代码网推荐   <asp:ServiceReference path="~/HelloWorldService.asmx" />
源代码网推荐   </Services>
源代码网推荐   </asp:ScriptManager>
源代码网推荐  放在<form>中了。
源代码网推荐  
源代码网推荐  改变好像挺大!
源代码网推荐  
源代码网推荐  http://www.cnblogs.com/seekinghu/archive/2006/11/07/552998.html
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华