ASP.NET AJAX Beta2 調用本地WebService的一些改變

來源:互聯網
上載者:User
ajax|asp.net|web

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

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"
         />
    </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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.