您的位置:首页 > 编程语言

VS2008 Web开发笔记 - Ajax 客户端编程 (一)

2010-03-18 16:25 417 查看
序:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  笔记 Ajax Web
相关文章推荐