DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> ASP.NET AJAX Beta2 調用本地WebService的一些改變
ASP.NET AJAX Beta2 調用本地WebService的一些改變
編輯:AJAX詳解     
 

發現最新版本的改動很大,下面就測試情況作一下說明(這裡借用官方的例子):

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved