VS2008 Web開發筆記 - Ajax 客戶端編程 (一)

序:
     VS2008的新功能讓我振奮。對VS2008的學習興趣愈來愈大,今天開始我會按部就班地學習VS2008的Web開發。由於本人的專長是開發B/S結 構應用開發。因此在這一系列中沒有關於winFrom的內容,而且本人暫時也不對WPF作過多的研究(感受它離實際應用還比較遠,並且我的精力有限)。
     在這一系列中將會設計的內容有 Asp.Net Ajax 編程(客戶端,服務器端),LINQ,WCF和WF。在開始前請務必安裝VS2008,目前只有英文版下載。不然後果本人不負責。
 
第一課:使用Asp.Net Ajax框架調用WebService
     仍是從經典的HelloWorld開始。新建工程後,在工程內添加一個WebService。
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
/// Summary description for WSHellowWorld
/// </summary>
[WebService(Namespace = " http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WSHellowWorld : System.Web.Services.WebService
{
    public WSHellowWorld()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}
取消[System.Web.Script.Services.ScriptService] 行前的註釋,這樣該WS就能暴露給客戶端Ajax Library訪問。
       再新建一個Aspx頁面,在From中放入一個ScriptManager控件。
<asp:ScriptManager ID="ScriptManager1" runat="server" >
        <Services>
            <asp:ServiceReference  Path="~/WebService/WSHellowWorld.asmx"/>
        </Services>
    </asp:ScriptManager>
爲ScriptManager 控件增長Service關聯。從而能夠直接訪問WS並調用其中方法。
而且依靠VS2008中的自感應系統,在編輯JavaScript腳本時能很是迅速的幫您定位到WSHelloWorld類,而且找到你須要的方法。
<script. type="text/javascript">     function TransferWSHelloWord()     {         var CallBack=function(result)         {             $get("Text1").value=result.toString();         }         //這裏實現調用WebService,經過VS2008強大的編輯器,你能夠輕鬆看到HelloWorld方法須要的參數。          WSHellowWorld.HelloWorld(CallBack,OnFailed,OnTimeOut);     }         function OnFailed(result)     {               var msg=result.get_exceptionType() + "\r\n";        msg += result.get_message() + "\r\n";        msg += result.get_stackTrace();        alert(msg);     }         //超時處理     function OnTimeOut(result)     {        alert("Timeout :" + result);     }     </script>
相關文章
相關標籤/搜索