当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET入门教程:Repeater控件

ASP.NET
Validation服务器控件:ValidationSummary控件
ASP.NET教程:URL重写的实现
ASP.NET巧妙实现无刷新更换CSS样式
ASP.NET教程:eval()函数详解
LINQ学习笔记:结构化且类型安全的查询
LINQ学习笔记:Lambda表达式
LINQ学习笔记:复合查询和Lambda表达式语法
LINQ学习笔记:查询是怎么执行的
LINQ学习笔记:子查询和延迟执行
LINQ学习笔记:创建更加复杂查询的策略
LINQ学习笔记:对象初始化器
LINQ学习笔记:解释查询(Interpreted Queries)
LINQ学习笔记:表达式树
LINQ学习笔记:过滤Filtering
LINQ学习笔记:选取Select
LINQ学习笔记:Join和Group Join
LINQ学习笔记:排序Ordering
LINQ学习笔记:分组Grouping
LINQ学习笔记:Set操作符
LINQ学习笔记:转换方法

ASP.NET入门教程:Repeater控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 49 ::
收藏到网摘: n/a

前面讲述了把XML文件绑定到列表控件的方法,下面我们来看一种新的控件。

Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件。

实例

Repeater 控件
带有 <AlternatingItemTemplate> 的 Repeater 控件
带有 <SeparatorTemplate> Repeater 控件

把 DataSet 绑定到 Repeater 控件

Repeater 空间用于显示重复的项目列表,这些项目被限制在该控件。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。这里,我们将展示如何把 XML 文件绑定到一个 Repeater 控件。本文由软晨学习网ruanchen.com整理发布!转载请注明出处,谢谢!

我们将在例子中使用下面的 XML 文件("cdcatalog.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

首先,导入 "System.Data" 命名空间。我们需要此命名空间与 DataSet 对象一同工作。在 .aspx 页面的顶部包含下面这条指令:看到本信息,说明该文章来源于软晨学习网www.ruanchen.com,如果文章不完整请到软晨学习网ruanchen.com浏览!

<%@ Import Namespace="System.Data" %>

接下来,为这个 XML 文件创建一个 DataSet,并把此 XML 文件在页面首次加载时载入 DataSet:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub

然后我们在 .aspx 页面中创建一个 Repeater 控件。<HeaderTemplate> 元素中的内容在输出中仅出现一次,而 <ItemTemplate> 元素的内容会对应 DataSet 中的 "record" 重复出现,最后,<FooterTemplate> 的内容在输出中仅出现一次:

<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

然后我们添加可创建 DataSet 的脚本,并把这个 mycdcatalog DataSet 绑定到 Repeater 控件。我们同样用 HTML 标签来填充这个 Repeater 控件,并通过 <%#Container.DataItem("fieldname")%> 方法把数据项目绑定到 <ItemTemplate> 部分内的单元格:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

使用 <AlternatingItemTemplate>

您可以在 <ItemTemplate> 元素后添加 <AlternatingItemTemplate> 元素,这样就可以描述交替行的外观了。在下面的例子中,该表格中每隔一行就会显示为浅灰色的背景:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

使用 <SeparatorTemplate>

<SeparatorTemplate> 元素能够用于描述每个记录之间的分隔符。下面的例子在每个表格行之间插入了一条水平线:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>