当前位置导航:炫浪网>>网络学院>>网页制作>>AJAX教程

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

发现最新版本的改动很大,下面就测试情况作一下说明(这里借用官方的例子):

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 = "")]
    [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"  "">

<html xmlns="">

  <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>中了。

改变好像挺大!

返回炫浪学院
相关内容
赞助商链接