ASP.NET Repeater 控件

asp.net web forms - repeater 控件

repeater 控件用于显示被绑定在该控件上的项目的重复列表。

绑定 dataset 到 repeater 控件

repeater 控件用于显示被绑定在该控件上的项目的重复列表。repeater 控件可被绑定到数据库表、xml 文件或者其他项目列表。在这里,我们将演示如何绑定 xml 文件到 repeater 控件。

在我们的实例中,我们将使用下面的 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>

查看这个 xml 文件:cdcatalog.xml

首先,导入 "system.data" 命名空间。我们需要该命名空间与 dataset 对象一起工作。 把下面这条指令包含在 .aspx 页面的顶部:

<%@ 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>


相关文章