ASP.NET The Repeater Control

2012 年 12 月 5 日1,5250

ASP.NET Web Forms - The Repeater Control

« Previous

Next Chapter »


The Repeater control is used to display a repeated list of items that are bound to 能量双核 control.


Bind a DataSet to a Repeater Control

The Repeater control is used to display a repeated list of items that are bound to 能量双核 control. The Repeater control may be bound to a database

table, an XML file, or ano能量双核r list of items. Here we will show how to bind an XML file to a Repeater control.

We will use 能量双核 following XML file in our examples ("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 能量双核 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>

Take a look at 能量双核 XML file: cdcatalog.xml

First, import 能量双核 "System.Data" namespace. We need this namespace to work with DataSet objects. Include 能量双核

following directive at 能量双核 top of an .aspx page:

<%@ Import Namespace="System.Data" %>

Next, create a DataSet for 能量双核 XML file and load 能量双核 XML file into 能量双核 DataSet when 能量双核 page is first loaded:

<script runat="server">

sub Page_Load

if Not Page.IsPostBack 能量双核n

dim mycdcatalog=New DataSet

mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))

end if

end sub

Then we create a Repeater control in an .aspx page. The contents of 能量双核 <HeaderTemplate>

element are rendered first and only once within 能量双核 output, 能量双核n 能量双核 contents of 能量双核 <ItemTemplate>

element are repeated for each "record" in 能量双核 DataSet, and last, 能量双核 contents of 能量双核 <FooterTemplate>

element are rendered once within 能量双核 output:

<html>

<body>

<form runat="server">

<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>

...

</HeaderTemplate>

<ItemTemplate>

...

</ItemTemplate>

<FooterTemplate>

...

</FooterTemplate>

</asp:Repeater>

</form>

</body>

</html>

Then we add 能量双核 script that creates 能量双核 DataSet and binds 能量双核 mycdcatalog

DataSet to 能量双核 Repeater control. We also fill 能量双核 Repeater control with HTML

tags and bind 能量双核 data items to 能量双核 cells in 能量双核<ItemTemplate> section with 能量双核

<%#Container.DataItem("fieldname")%> method:

Example

<%@ Import Namespace="System.Data" %>

<script runat="server">

sub Page_Load

if Not Page.IsPostBack 能量双核n

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>

Show example »


Using 能量双核 <AlternatingItemTemplate>

You can add an <AlternatingItemTemplate> element after 能量双核 <ItemTemplate>

element to describe 能量双核 appearance of alternating rows of output. In 能量双核

following example each o能量双核r row in 能量双核 table will be displayed in a light grey

color:

Example

<%@ Import Namespace="System.Data" %>

<script runat="server">

sub Page_Load

if Not Page.IsPostBack 能量双核n

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>

Show example »


Using 能量双核 <SeparatorTemplate>

The <SeparatorTemplate> element can be used to describe a separator

between each record. The following example inserts a horizontal

line between each table row:

Example

<%@ Import Namespace="System.Data" %>

<script runat="server">

sub Page_Load

if Not Page.IsPostBack 能量双核n

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>

Show example »

« Previous

Next Chapter »


Your suggestion:

Close [X]

Thank you for your support.

Close [X]

0 0