使用ASP.NET Atlas ListView控件显示列表数据

English Version: http://dflying.dflying.net/1/archive/113_display_listible_data_using_aspnet_atlas_listview_control.html

在这个系列中,我将介绍一些Atlas Sys.UI.Data中较高级的控件,包括:

 

  1. Sys.UI.Data.ListView使用ASP.NET Atlas ListView控件显示列表数据
  2. Sys.UI.Data.ItemView使用ASP.NET Atlas ItemView控件显示集合中的单个数据
  3. Sys.UI.Data.DataNavigator使用 ASP.NET Atlas PageNavigator控件实现客户端分页导航 
  4. Sys.UI.Data.SortBehavior使用ASP.NET Atlas SortBehavior实现客户端排序
  5. Sys.UI.Data.XSLTView使用ASP.NET Atlas XSLTView控件用XSLT修饰并显示XML数据
这篇是其中的第一篇: 使用ASP.NET Atlas ListView控件显示列表数据

 

在目前的大部分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中有如下模版:

  1. layoutTemplate:这个模版用来渲染包含列表项目的容器(例如使用<table>标记),列表的头部(例如使用<thead>标记),尾部等。您必须为ListView指定一个layoutTemplate。而且这个模版必须包含一个itemTemplate模版,也可选包含一个separatorTemplate模版。
  2. itemTemplate:这个模版用来渲染列表中的一个项目(例如使用<tr>标记)。这个模版必须被置于layoutTemplate中。
  3. separatorTemplate:这个模版用来渲染列表中的项目之间的分隔元素(例如使用<hr>标记)。这个模版必须被置于layoutTemplate中。
  4. emptyTemplate.:这个模版用来渲染没有项目存在时的ListView。此时可能与该ListView相关的DataSource对象中没有项目,或是正在从服务器中取得的过程中。

ListView中还有一些属性:

  1. itemCssClass:指定项目条目的css class。
  2. alternatingItemCssClass:指定间隔的项目条目的css class。
  3. selectedItemCssClass:指定被选中的项目条目的css class。
  4. separatorCssClass:指定分隔元素的css class。
  5. itemTemplateParentElementId:这个属性指定了itemTemplate和separatorTemplate的父元素。这样itemTemplate和separatorTemplate元素就可以在其中被重复渲染。

ListView还有一些继承于Sys.UI.Data.DataControl基类的方法/属性,因为在下面的代码中我们不需要使用,这里暂且略过。如果您感兴趣,可以参考这篇文章:使用ASP.NET Atlas ItemView控件显示集合中的单个数据。OK,让我们通过一个实例来说明如何使用ListView控件:

首先,我们编写一个返回.NET中DataTable的Web Service。注意到在这里将继承于Microsoft.Web.Services.DataService基类,并且为service方法加上定义在名称空间System.ComponentModel中的属性DataObjectMethod。在service方法的开头,我们使用System.Threading.Thread.Sleep(2000)来模拟2秒钟的网络延迟,以便可以看到emptyTemplate中的内容。

None.gif [WebService(Namespace  =   " http://tempuri.org/ " )]
None.gif[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
ExpandedBlockStart.gifContractedBlock.gif
public   class  MyService  : Microsoft.Web.Services.DataService  dot.gif {
InBlock.gif
InBlock.gif    [DataObjectMethod(DataObjectMethodType.Select)]
InBlock.gif    
public DataTable GetListData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Threading.Thread.Sleep(
2000);
InBlock.gif        
InBlock.gif        DataTable dt 
= new DataTable("MyListData");
InBlock.gif        dt.Columns.Add(
"Name"typeof(string));
InBlock.gif        dt.Columns.Add(
"Email"typeof(string));
InBlock.gif        DataRow newRow;
InBlock.gif        
for (int i = 0; i < 5++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newRow 
= dt.NewRow();
InBlock.gif            newRow[
"Name"= string.Format("Dflying {0}", i);
InBlock.gif            newRow[
"Email"= string.Format("Dflying{0}@dflying.net", i);
InBlock.gif            dt.Rows.Add(newRow);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return dt;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

然后,添加一些 ASP.NET 页面中必须的控件 / 标记:
None.gif < atlas:ScriptManager  ID ="ScriptManager1"  runat ="server"   />
None.gif
<!--  Element for myList (container)  -->
None.gif
< div  id ="myList" ></ div >
None.gif
<!--  Layout Elements  -->
None.gif
< div  style ="display: none;" >
None.gif
</ div >

在上面的标记中,我们加入了三个标记:一个必须的ScriptManager控件。一个id为myList的div,用来让Atlas把渲染后的ListView放置于这里。一个隐藏的div,用于定义我们的模版。这个隐藏div中的元素在页面上是不可见的,只是用来提供给Atlas必要的模版。

我们在这个隐藏的div中加入如下ListView的模版:

None.gif <!--  Layout Template  -->
None.gif
< table  id ="myList_layoutTemplate"  border ="1"  cellpadding ="3" >
None.gif    
< thead >
None.gif        
< tr >
None.gif            
< td >< span > No. </ span ></ td >
None.gif            
< td >< span > Name </ span ></ td >
None.gif            
< td >< span > Email </ span ></ td >
None.gif        
</ tr >
None.gif    
</ thead >
None.gif    
<!--  Repeat Template  -->
None.gif    
< tbody  id ="myList_itemTemplateParent" >
None.gif        
<!--  Repeat Item Template  -->
None.gif        
< tr  id ="myList_itemTemplate" >
None.gif            
< td >< span  id ="lblIndex"   /></ td >
None.gif            
< td >< span  id ="lblName"   /></ td >
None.gif            
< td >< span  id ="lblEmail"   /></ td >
None.gif        
</ tr >
None.gif        
<!--  Separator Item Template  -->
None.gif        
< tr  id ="myList_separatorTemplate" >
None.gif            
< td  colspan ="3" > Separator </ td >
None.gif        
</ tr >
None.gif    
</ tbody >
None.gif
</ table >
None.gif
<!--  Empty Template  -->
None.gif
< div  id ="myList_emptyTemplate" >
None.gif    No Data
None.gif
</ div >

上面的代码中您可以看到我提到的所有四种模版。另外还要指定每一个模版一个id,将用于下面的Atlas脚本声明中。在这个例子中我将以HTML Table的形式渲染这个ListView,很抱歉分隔元素将会很丑陋(一个空行)。

最后在页面中添加Atlas脚本声明:

None.gif < dataSource  id ="listDataSource"  autoLoad ="true"  serviceURL ="MyService.asmx"   />
None.gif
None.gif
< listView  id ="myList"  itemTemplateParentElementId ="myList_itemTemplateParent" >
None.gif    
< bindings >
None.gif        
< binding  dataContext ="listDataSource"  dataPath ="data"  property ="data"   />
None.gif    
</ bindings >
None.gif    
< layoutTemplate >
None.gif        
< template  layoutElement ="myList_layoutTemplate"   />
None.gif    
</ layoutTemplate >
None.gif    
< itemTemplate >
None.gif        
< template  layoutElement ="myList_itemTemplate" >
None.gif            
< label  id ="lblIndex" >
None.gif                
< bindings >
None.gif                    
< binding  dataPath ="$index"  transform ="Add"  property ="text" />
None.gif                
</ bindings >
None.gif            
</ label >
None.gif            
< label  id ="lblName" >
None.gif                
< bindings >
None.gif                    
< binding  dataPath ="Name"  property ="text"   />     
None.gif                
</ bindings >
None.gif            
</ label >
None.gif            
< label  id ="lblEmail" >
None.gif                
< bindings >
None.gif                    
< binding  dataPath ="Email"  property ="text"   />
None.gif                
</ bindings >
None.gif            
</ label >                     
None.gif        
</ template >
None.gif    
</ itemTemplate >
None.gif    
< separatorTemplate >
None.gif        
< template  layoutElement ="myList_separatorTemplate"   />
None.gif    
</ separatorTemplate >
None.gif    
< emptyTemplate >
None.gif        
< template  layoutElement ="myList_emptyTemplate" />
None.gif    
</ emptyTemplate >
None.gif
</ 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)。

大功告成,运行一下吧。

装载中:
listview1.JPG

装载完成:
listview2.JPG

示例代码可以在此下载:http://files.cnblogs.com/dflying/AtlasListViewDemo.zip 

转载于:https://www.cnblogs.com/dflying/archive/2006/04/08/370331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值