今天花了一点时间来大致学习了一下repeater的使用,觉得有些知识还是要记下了,备忘。
模板 |
说明 |
AlternatingItemTemplate |
与 ItemTemplate 元素类似,但在 Repeater 控件中隔行(交替项)呈现一次。通过设置 AlternatingItemTemplate 元素的样式属性,可以为其指定不同的外观。 |
FooterTemplate |
在所有数据绑定行呈现之后呈现一次的元素。典型的用途是关闭在 HeaderTemplate 项中打开的元素(使用 </table> 这样的标记)。 注意 FooterTemplate 不能是数据绑定的。 |
HeaderTemplate |
在所有数据绑定行呈现之前呈现一次的元素。典型的用途是开始一个容器元素(如表)。 注意 HeaderTemplate 项不能是数据绑定的。 |
ItemTemplate |
为数据源中的每一行都呈现一次的元素。若要显示 ItemTemplate 中的数据,请声明一个或多个 Web 服务器控件并设置其数据绑定表达式以使其计算为 Repeater 控件(即容器控件)的 DataSource 中的字段。以下示例显示一个示例声明,它显示包含 Label 控件中的第一个名称的字段。 First Name: <asp:Label runat="server" Text="<%# Container.DataItem.FirstName %>" /> |
SeparatorTemplate |
在各行之间呈现的元素,通常是分行符(<br> 标记)、水平线(<hr> 标记)等。 注意 SeparatorTemplate 项不能是数据绑定的。 |
绑定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中的每一条“记录”被重复(循环)输出,最后给出的是<FooterTemplate>元素中的内容,仅输出一次:
<html>
<body><form runat="server">
<asp:Repeater id="cdcatalog" runat="server"><HeaderTemplate>
...
</HeaderTemplate><ItemTemplate>
...
</ItemTemplate><FooterTemplate>
...
</FooterTemplate></asp:Repeater>
</form></html>
</body>
然后我们加入脚本,创建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></html>
</body>
--------------------------------------------------------------------------------
使用<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></html>
</body>
--------------------------------------------------------------------------------
使用<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></html>
</body>