# 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](/try/demo_source/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>
```
[演示實例 ?](/try/showaspx.php?filename=demo_repeater1)
## 使用 <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>
```
[演示實例 ?](/try/showaspx.php?filename=demo_repeater2)
## 使用 <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>
```
[演示實例 ?](/try/showaspx.php?filename=demo_repeater3)
- C# 基礎
- C# 簡介
- C# 環境
- C# 程序結構
- C# 基本語法
- C# 數據類型
- C# 類型轉換
- C# 變量
- C# 常量
- C# 運算符
- C# 判斷
- C# 循環
- C# 封裝
- C# 方法
- C# 可空類型(Nullable)
- C# 數組(Array)
- C# 字符串(String)
- C# 結構(Struct)
- C# 枚舉(Enum)
- C# 類(Class)
- C# 繼承
- C# 多態性
- C# 運算符重載
- C# 接口(Interface)
- C# 命名空間(Namespace)
- C# 預處理器指令
- C# 正則表達式
- C# 異常處理
- C# 文件的輸入與輸出
- C# 高級
- C# 特性(Attribute)
- C# 反射(Reflection)
- C# 屬性(Property)
- C# 索引器(Indexer)
- C# 委托(Delegate)
- C# 事件(Event)
- C# 集合(Collection)
- C# 泛型(Generic)
- C# 匿名方法
- C# 不安全代碼
- C# 多線程
- ASP.NET 簡介
- Web Pages 教程
- ASP.NET Web Pages - 教程
- ASP.NET Web Pages - 添加 Razor 代碼
- ASP.NET Web Pages - 頁面布局
- ASP.NET Web Pages - 文件夾
- ASP.NET Web Pages - 全局頁面
- ASP.NET Web Pages - HTML 表單
- ASP.NET Web Pages - 對象
- ASP.NET Web Pages - 文件
- ASP.NET Web Pages - 幫助器
- ASP.NET Web Pages - WebGrid 幫助器
- ASP.NET Web Pages - Chart 幫助器
- ASP.NET Web Pages - WebMail 幫助器
- ASP.NET Web Pages - PHP
- ASP.NET Web Pages - 發布網站
- Razor 教程
- ASP.NET Razor - 標記
- ASP.NET Razor - C# 和 VB 代碼語法
- ASP.NET Razor - C# 變量
- ASP.NET Razor - C# 循環和數組
- ASP.NET Razor - C# 邏輯條件
- ASP.NET Razor - VB 變量
- ASP.NET Razor - VB 循環和數組
- ASP.NET Razor - VB 邏輯條件
- MVC 教程
- ASP.NET MVC 教程
- ASP.NET MVC - Internet 應用程序
- ASP.NET MVC - 應用程序文件夾
- ASP.NET MVC - 樣式和布局
- ASP.NET MVC - 控制器
- ASP.NET MVC - 視圖
- ASP.NET MVC - SQL 數據庫
- ASP.NET MVC - 模型
- ASP.NET MVC - 安全
- ASP.NET MVC - HTML 幫助器
- ASP.NET MVC - 發布網站
- Web Forms 教程
- ASP.NET Web Forms - 教程
- ASP.NET Web Forms - HTML 頁面
- ASP.NET Web Forms - 服務器控件
- ASP.NET Web Forms - 事件
- ASP.NET Web Forms - HTML 表單
- ASP.NET Web Forms - 維持 ViewState
- ASP.NET Web Forms - TextBox 控件
- ASP.NET Web Forms - Button 控件
- ASP.NET Web Forms - 數據綁定
- ASP.NET Web Forms - ArrayList 對象
- ASP.NET Web Forms - Hashtable 對象
- ASP.NET Web Forms - SortedList 對象
- ASP.NET Web Forms - XML 文件
- ASP.NET Web Forms - Repeater 控件
- ASP.NET Web Forms - DataList 控件
- ASP.NET Web Forms - 數據庫連接
- ASP.NET Web Forms - 母版頁
- ASP.NET Web Forms - 導航
- Web Pages 參考手冊
- ASP.NET Web Pages - 類
- ASP.NET Web Pages - WebSecurity 對象
- ASP.NET Web Pages - Database 對象
- ASP.NET Web Pages - WebMail 對象
- ASP.NET Web Pages - 更多幫助器
- MVC - 參考手冊
- Web Forms 參考手冊
- ASP.NET Web Forms - HTML 服務器控件
- ASP.NET Web Forms - Web 服務器控件
- ASP.NET Web Forms - Validation 服務器控件
- 免責聲明