在目前的大部分web程序中,我们都需要显示给用户一些列表数据。asp.net中的gridview服务器控件提供了这种功能,atlas中的客户端控件listview也可以在客户端提供类似功能,并以ajax方式运行。 虽然您可以使用传统的gridview控件加上atlas的updatepanel提供同样的ajax运行方式,但是这种实现方式较低效,也不是“纯粹”的atlas方法。推荐的方法是采用atlas的客户端控件listview来代替。不要担心,atlas的listview控件和gridview一样简单,而其二者在很多概念方面是相似的,例如itemtemplate。但是需要注意的是目前ide中并没有提供对atlas脚本的智能感知功能,加之atlas脚本也没有编译时期检查,所以在书写代码的时候应该格外小心。
使用listview的时候应该提供给其一些必要的模版(template),以告诉atlas应该如何渲染您的内容。listview中有如下模版:
在上面的标记中,我们加入了三个标记:一个必须的scriptmanager控件。一个id为mylist的div,用来让atlas把渲染后的listview放置于这里。一个隐藏的div,用于定义我们的模版。这个隐藏div中的元素在页面上是不可见的,只是用来提供给atlas必要的模版。
我们在这个隐藏的div中加入如下listview的模版:
<!-- layout template -->
<table id="mylist_layouttemplate" border="1" cellpadding="3">
<thead>
<tr>
<td> <span>no. </span> </td>
<td> <span>name </span> </td>
<td> <span>email </span> </td>
</tr>
</thead>
<!-- repeat template -->
<tbody id="mylist_itemtemplateparent">
<!-- repeat item template -->
<tr id="mylist_itemtemplate">
<td> <span id="lblindex" /> </td>
<td> <span id="lblname" /> </td>
<td> <span id="lblemail" /> </td>
</tr>
<!-- separator item template -->
<tr id="mylist_separatortemplate">
<td colspan="3">separator </td>
</tr>
</tbody>
</table>
<!-- empty template -->
<div id="mylist_emptytemplate">
no data
</div>
上面的代码中您可以看到我提到的所有四种模版。另外还要指定每一个模版一个id,将用于下面的atlas脚本声明中。在这个例子中我将以html table的形式渲染这个listview,很抱歉分隔元素将会很丑陋(一个空行)。
最后在页面中添加atlas脚本声明:
<datasource id="listdatasource" autoload="true" serviceurl="myservice.asmx" />
<listview id="mylist" itemtemplateparentelementid="mylist_itemtemplateparent">
<bindings>
<binding datacontext="listdatasource" datapath="data" property="data" />
</bindings>
<layouttemplate>
<template layoutelement="mylist_layouttemplate" />
</layouttemplate>
<itemtemplate>
<template layoutelement="mylist_itemtemplate">
<label id="lblindex">
<bindings>
<binding datapath="$index" transform="add" property="text"/>
</bindings>
</label>
<label id="lblname">
<bindings>
<binding datapath="name" property="text" />
</bindings>
</label>
<label id="lblemail">
<bindings>
<binding datapath="email" property="text" />
</bindings>
</label>
</template>
</itemtemplate>
<separatortemplate>
<template layoutelement="mylist_separatortemplate" />
</separatortemplate>
<emptytemplate>
<template layoutelement="mylist_emptytemplate"/>
</emptytemplate>
</listview>
这里我添加了一个atlas客户端datasource对象以从web service中取得数据。这里我们暂且不多谈datasource(可能在后续文章中有所介绍)。让我们来看一下listview相关的定义:在listview的定义中,我们指定了itemtemplateparentelementid属性。然后在listview的内部定义了一个binding段,用来把datasource中取得的数据与这个listview绑定起来。我们还定义了四个模版段,每个模版段都用layoutelement与上面定义过的四种模版关联。注意到在layouttemplate模版中的第一个label控件,我们在其绑定中指定了一个add transformer以将从0开始的顺序变为从1开始(关于atlas transformer,请参考我的这篇文章:http://dflying.cnblogs.com/archive/2006/04/05/367908.html)。
大功告成,运行一下吧。
装载中:
装载完成:
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器