Repeater 控件用于顯示重復的項目列表,這些項目被限制在該控件。
ASP.NET - Repeater 控件
實例
- Repeater 控件
- 帶有 <AlternatingItemTemplate> 的 Repeater 控件
- 帶有 <SeparatorTemplate> 的 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>
顯示這個例子