======================================================
注:本文源代码点此下载
======================================================
[翻译]asp.net ajax xml-script教程(三)
posted on 2007-01-27 18:43 webabcd 阅读(2846) 评论(8)编辑 收藏
原文地址:http://dotnetslackers.com/articles/atlas/xml_script_tutorial_part3.aspx
asp.net ajax xml-script教程(三)
原文发布日期:2007.01.01
翻译:webabcd
介绍
在xml-script教程的第一部分(译者注:中文在这里)和第二部分(译者注:中文在这里)中我们介绍了声明程序模型和如何通过客户端的microsoft ajax对象处理事件。处理事件可以调用一段javascript函数,也可以使用action。在本文(第三部分)中我们将谈谈invokemethod action,它允许我们以声明的方式调用一个方法。
invokemethod action
invokemethod action可以调用一个方法,该方法必须是客户端对象已有的一个类型描述符。在接下来的示例中,我们将介绍一个action,它就是futures ctp下的sys.preview.net.servicemethodrequest。这个类抽象出了一个异步调用web service的方法,并且有一个允许在xml-script中使用的类型描述符。也就是说我们可以不用写一行javascript就能调用一个web service上的方法并且得到返回结果。
示例
我们将调用web service上的一个返回当前时间的方法,然后把这个时间显示到一个label上。
首先我们要先创建一个web service。在你的安装了ajax-ctp的站点中新建一个名为datetimeservice.asmx的web service,其代码如下:
@ webservice language="c#" class="datetimeservice" %>
using system;
using system.web;
using system.web.services;
using system.web.services.protocols;
using system.web.script.services;
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
[scriptservice]
public class datetimeservice : system.web.services.webservice
{
[webmethod]
[scriptmethod]
public string gettimeasstring()
{
return datetime.now.toshorttimestring();
}
}
注意:datetimeservice类被scriptservice属性所描述,它通知asp.net ajax去生成一个web service的客户端代理类。这个代理类允许我们使用客户端的javascript来调用以scriptmethod描述的方法(就是像gettimeasstring的这种方法)
现在让我们来看看xml-script中的代码,为了使用xml-script这个特性,你需要在你的web站点中引用microsoft.web.preview.dll,然后在scriptmanager内加载previewscript.js文件
@ 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 runat="server">
title>declarative webservice calltitle>
head>
body>
form id="form1" runat="server">
asp:scriptmanager id="scriptmanager1" runat="server">
services>
asp:servicereference path="datetimeservice.asmx" />
services>
scripts>
asp:scriptreference assembly="microsoft.web.preview" name="previewscript.js" />
scripts>
asp:scriptmanager>
h2>
span>time on web server: span>
asp:label id="lbltime" runat="server">asp:label>
h2>
script type="text/xml-script">
page xmlns="http://schemas.microsoft.com/xml-script/2005">
components>
label id="lbltime" />
timer id="thetimer" interval="2000" enabled="true">
tick>
invokemethodaction target="timeservicemethod" method="invoke">
parameters usercontext="" />
invokemethodaction>
tick>
timer>
servicemethodrequest id="timeservicemethod"
url="datetimeservice.asmx"
methodname="gettimeasstring">
completed>
setpropertyaction target="lbltime"
property="text"
>
bindings>
binding datacontext="timeservicemethod"
datapath="result"
property="value"
/>
bindings>
setpropertyaction>
completed>
servicemethodrequest>
components>
page>
script>
form>
body>
html>
让我们来看一看其中的xml-script代码块。label元素关联到span标签,sys.preview.ui.label控件的实例在runtime的时候被创建。后面是一个timer元素,使用它的时候会创建一个sys.preview.timer类的实例,timer暴露了一个interval属性用来设置时间的间隔,每过一个间隔时间timer对象的tick事件将被触发。
关于invokemethod action的更多内容
tick事件可以用来处理一个invokemethod action,调用它的方法是invoke(在method属性中指定),调用对象的id是timeservicemethod,用target属性指出。在invokemethodaction元素内有一个parameters元素,该元素用于指定传递给方法的参数。如果要指定一个参数,我们要在parameters元素中增加参数名称和参数值。在上面的例子中,我们指定了一个被称作usercontext的参数并设置其值为空。
id为timeservicemethod的servicemethodrequest元素被解析成一个sys.preview.net.servicemethodrequest类的实例。url和methodname属性分别指定web service的路径及调用方法的名称。
在servicemethodrequest元素内我们会发现一个completed节点。当servicemethodrequest类成功的异步调用了web方法后将触发completed事件,我们使用setproperty action来处理这个事件,以设置label的text为web方法返回的格式化了当前日期的字符串。servicemethodrequest类还暴露了其它一些事件,如timeout和error,当异步请求的处理期间发生了过期或错误的时候将会触发它们。
绑定
在setpropertyaction元素内声明一个绑定元素是一件让人感兴趣的事。因为通过web方法返回的字符串是存储在servicemethodrequest类的result属性里的,所以我们不能在setpropertyaction的value属性中指定它,而是需要声明式的访问result属性。
要这么做,就需要使用绑定,建立相同对象或不同对象的两个属性之间的关系,可以使得当一个属性发生改变的时候,另一个属性也自动的改变。如果你注意看了示例中的绑定声明,你会看到datacontext属性指向timeservicemethod,datapath属性指向result。最后property属性设置成value。setpropertyaction元素的value属性(或者说是setpropertyaction类的value属性)将总是和servicemethodrequest类的result属性有相同的值。作为结果,label的text将通过result属性被设置成返回的字符串。
如果你还不明白绑定是如何工作的,不要着急,因为我们将在这个系列教程的下一部分中解释它们。
总结
在本文(教程的第三部分)中我们介绍了invokemethod action,这个action可以调用一个在客户端对象中的类型描述符中暴露的方法。在这个例子中,我们使用了invokemethod action和setproperty action去每隔一段时间调用一次web方法,并在label中显示返回的字符串,所有这些都不用写一行javascript。
在本教程的下一部分中,我们将懂得如何创建自定义的action。
绿色通道:好文要顶关注我收藏该文与我联系
feedback
回复 引用
2008-11-02 19:17 by hehe12[未注册用户]
说实在,asp.net ajax xml让我很恶心,这还是javascript吗?
非常怀疑ms的居心,是不是让程序员都忘了js,全用这种怪里怪气的语法.
#2楼[楼主]
回复 引用 查看
@hehe12
基于xml的编程方式,也许是趋势吧
flex,silverlight都是如此
回复 引用 查看
用xml-script写了个小程序,然后就头痛了,太累人了,没有感知,甚至没关键字变色,一不小心打错个字母,整个程序就跑不了,还是老老实实用js吧
#4楼[楼主]
回复 引用 查看
@supercpu
是啊,不过如果正式版出来肯定会有改进的
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
最新it新闻:
·android平台12月广告浏览份额51.6% 超越ios
» 更多新闻...
最新知识库文章:
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/