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>
相关文章
相关标签/搜索