當前位置:
首頁 > 知識 > Web Forms Repeater 控制項

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>

Web Forms Repeater 控制項



使用 <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>

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

AppML案例研究之應用程序模型
ionic icon(圖標)

TAG:程序員小新人學習 |