非常教程

ASP.NET 教程教程

ASP.NET Repeater 控件

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>

演示实例 »

ASP.NET Repeater 控件
ASP.NET 教程

ASP.NET 是微软公司提出的一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。ASP.NET 支持三种不同的开发模式:单页面模式、MVC 模式、事件驱动模式。

ASP.NET 教程目录

1.ASP.NET Web Pages Razor
2.ASP.NET Web Pages 教程
3.ASP.NET
4.ASP.NET 教程
5.ASP.NET Web Pages 布局
6.ASP.NET Web Pages 对象
7.ASP.NET Web Pages HTML 表单
8.ASP.NET Web Pages – 发布
9.ASP.NET Web Pages Email
10.ASP.NET Web Pages WebGrid
11.ASP.NET Web Pages 文件
12.ASP.NET Razor 语法
13.ASP.NET Razor 标记
14.ASP.NET WebPages 帮助器参考手册
15.ASP.NET Web Pages WebMail 参考手册
16.ASP.NET Web Pages Database 参考手册
17.ASP.NET Web Pages WebSecurity 参考手册
18.ASP.NET Web Pages 类参考手册
19.ASP.NET Web 的 C# 和 VB 实例
20.ASP.NET Razor C# 循环和数组
21.ASP.NET Razor C# 变量
22.ASP.NET Razor VB 逻辑
23.ASP.NET Razor VB 循环和数组
24.ASP.NET Razor VB 变量
25.ASP.NET Razor C# 逻辑
26.ASP.NET MVC 模型
27.ASP.NET MVC 页面和布局
28.ASP.NET 事件句柄
29.ASP.NET 服务器控件
30.ASP.NET Web 页面
31.ASP.NET Web Forms 教程
32.ASP.NET MVC 参考手册
33.ASP.NET MVC – 发布
34.ASP.NET MVC HTML 帮助器
35.ASP.NET Repeater 控件
36.ASP.NET XML 数据绑定
37.ASP.NET SortedList
38.ASP.NET Hashtable
39.ASP.NET ArrayList
40.ASP.NET 数据绑定
41.ASP.NET Button 控件
42.ASP.NET TextBox 控件
43.ASP.NET ViewState
44.ASP.NET Web 表单
45.ASP.NET DataList 控件
46.ASP.NET 母版页
47.ASP.NET 数据库连接
48.ASP.NET 实例
49.ASP.NET 导航
50.ASP.NET HtmlAnchor 控件
51.ASP.NET HtmlGeneric 控件
52.ASP.NET HtmlForm 控件
53.ASP.NET HtmlButton 控件
54.ASP.NET HtmlInputHidden 控件
55.ASP.NET HtmlInputFile 控件
56.ASP.NET HtmlInputCheckBox 控件
57.ASP.NET HtmlInputButton 控件
58.ASP.NET HtmlImage 控件
59.ASP.NET AdRotator 控件
60.ASP.NET HTML 服务器控件
61.ASP.NET HtmlTextArea 控件
62.ASP.NET HtmlTableRow 控件
63.ASP.NET HtmlTableCell 控件
64.ASP.NET HtmlTable 控件
65.ASP.NET HtmlSelect 控件
66.ASP.NET HtmlInputText 控件
67.ASP.NET HtmlInputRadioButton 控件
68.ASP.NET HtmlInputImage 控件
69.ASP.NET Label 控件
70.ASP.NET ImageButton 控件
71.ASP.NET Image 控件
72.ASP.NET HyperLink 控件
73.ASP.NET DropDownList 控件
74.ASP.NET CheckBoxList 控件
75.ASP.NET CheckBox 控件
76.ASP.NET CalendarDay 控件
77.ASP.NET Calendar 控件
78.ASP.NET Button 控件
79.ASP.NET Style 控件
80.ASP.NET BulletedList 控件
81.ASP.NET RadioButtonList 控件
82.ASP.NET RadioButton 控件
83.ASP.NET PlaceHolder 控件
84.ASP.NET Panel 控件
85.ASP.NET Literal 控件
86.ASP.NET ListItem 控件
87.ASP.NET ListBox 控件
88.ASP.NET LinkButton 控件
89.ASP.NET RegularExpressionValidator 控件
90.ASP.NET RangeValidator 控件
91.ASP.NET CustomValidator 控件
92.ASP.NET CompareValidator 控件
93.ASP.NET Web 服务器控件
94.ASP.NET XML 控件
95.ASP.NET TextBox 控件
96.ASP.NET TableRow 控件
97.ASP.NET TableCell 控件
98.ASP.NET Table 控件
99.ASP.NET Validation 服务器控件
100.ASP.NET ValidationSummary 控件