Atlas绑定之通俗解释
引用别人的一个例子:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" />
<div>
Inputanintegerfrom1to5.<br/>
<input id="myTextBox"type="text"/><br/>
Selectanitem.<br/>
<select id="mySelect">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
<option value="4">value4</option>
<option value="5">value5</option>
</select>
</div>
</form>
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
<textBox id="myTextBox">
<bindings>
<binding dataContext="mySelect" dataPath="selectedValue" property="text" direction="inOut"/>
</bindings>
</textBox>
<select id="mySelect"/>
</components>
</page>
</script>
<!--
这里的direction的值是相对于myTextBox页言,如设为Out,表示系统仅监视从myTextBox出去的数据(OUT),
也就是在myTextBox中输入数据,myTextBox会变化,而反之却不然
上面的绑定<components></components>之间也可如下写:
<textBox id="myTextBox"/>
<select id="mySelect">
<bindings>
<binding dataContext="myTextBox" dataPath="text" property="selectedValue" direction="inOut"/>
</bindings>
</select>
也就是说,bindings是相对于其父节点控件而言(本例中的MyTextBox,设为A),其中的dataContext指的是被绑定的另一控件(设为B),
dataPath指的是被绑定的那个控件(B)的绑定属性,用以向A提供所需数据的;而Property指的是控件A的一个属性,这个属性必需是
要向B提供所需数据的数性,如果这个属性是嵌套的,那还要进一步指明其ProperKey属性(如Property="Style" ProperKey="color")
在此,myTextBox的property值(即text属性)如果发生了变化,即会连动dataContext中所指定的控件(myselect控件)的由dataPath所
指定的属性(selectedValue)的同步变化,同时,父控件(A)还具有监听控件B相应属性(selectedValue)数据变化情况的任务,一旦
发生变化,本身的相应属性值也会发生变化。
-->
</body>
</html>
以上的被绑定控件都是在客户端的,下面是一个客户端控件与web Service绑定的例子,例子来源于MS的atlas教程的一部分(绑定部分),由于为了说明事情,限于本人理解,可能有些说法表达不是很准确。
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>
1 <textBox id="SearchKey" />
2 <serviceMethod id="helloService"
3 url="HelloWorldService.asmx"
4 methodName="HelloWorld">
5 <bindings>
6 <binding dataContext="SearchKey"
7 dataPath="text"
8 property="parameters"
9 propertyKey="query" />
10 </bindings>
11 <completed>
12 <invokeMethod target="resultsBinding"
13 method="evaluateIn" />//访问方法(处理返回数据)
14 </completed>
15 </serviceMethod>
16 <button id="SearchButton">
17 <click>
18 <invokeMethod target="helloService"
19 method="invoke" />//访问方法(访问服务器)
20 </click>
21 </button>
22 <label id="results">
23 <bindings>
24 <binding id="resultsBinding"
25 dataContext="helloService"
26 dataPath="result"
27 property="text"
28 automatic="false" />
29 </bindings>
30 </label>
</components>
</page>
</script>
这是一段客户端与服务器绑定的典型例子.
2-15行,声明了一个ServerMethod,2-3行相当于对SERVER端的WEB SERVICE做了一个映射对象.
5-10行,对服务器端的helloService与本地SearchKey进行绑定,使searchKey.text=helloService.parameters.query.(这种写法只表示相互关系,下同)
11-14行,定义了当SERVER端产生completed事件后客户端要做的事情,这里是通过evaluatein方式引发resultsBinding的动作(由target所指定)
17-20行,定义了一个CLICK事件,并通过target指定要访问的目标(第2-15行定义的helloservice),访问方法为:invoke,由于被写在了<button>
</button>之间,因此这一事件是由button引起的
23-29行,定义了一个对results对象的绑定,由于要在第12行引用,所以添加了一个ID属性.绑定结果是:helloService.result=results.text
也就是说results.text要显示heleloService返回的结果,但由于随着请求的进程变化,helloService.result会有不同的结果值返回,
所以这里设定了automatic="false",来取消自动的联动,而由completed事件来控制数据的显示。
通过以上代码可以看到,控件的绑定是写在控件的定义里的,被定义的控件称为源控件,另一个则称为目标控件.目标控件通过dataContext
指定,并用dataPath指定目标控件被绑定的属性.Property用来指定源控件的绑定属性,如果有嵌套属性,则用properKey来指定.注意:如果
要引用嵌套属性,则不能将其当做目标控件.另外,与服务器间的数据转递,不能单靠绑定完成(不同于客户端之间控件绑定),必需借助事件
触发数据的流动.如这里的<click></click>事件使数据上传到服务器,而<completed>事件又使数据返回客户端,并引发绑定事件.
浙公网安备 33010602011771号