To display a repeated list of items that are bound to the control,the Repeater control is used.
Bind a DataSet to a Repeater Control
A D V E R T I S E M E N T
To display a repeated list of items that are bound to the control,the Repeater control is used. The Repeater control may be bound to an XML file, a database table, or another list of items. Here we will show you how to bind an XML file to a Repeater control.
We will use the following XML file in our examples ("cdcatalog.xml"):
At first, import the "System.Data" namespace and we need this namespace to work with DataSet objects. Then,at the top of an .aspx page include the following directive:
<%@ Import Namespace="System.Data" %>
Next, create a DataSet for the XML file and when the page is first loaded,load the XML file into the 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
Then we create a Repeater control in an .aspx page and the contents of the <HeaderTemplate> element are rendered first and only once within the output,then the contents of the <ItemTemplate> element are repeated for each "record" in the DataSet, and last,
the contents of the <FooterTemplate> element are rendered once within the output:
Then we add the script that creates the DataSet and then binds
the mycdcatalog DataSet to the Repeater control.With HTML tags,we also fill
the Repeater control and bind the data items to
the cells in the<ItemTemplate> section with the <%#Container.DataItem("fieldname")%>
method:
<%@ 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>
After the <ItemTemplate> element,you can add an <AlternatingItemTemplate> element to describe the appearance of alternating rows of output.In the example below each other row in the table will be displayed in a light grey color:
<%@ 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>
To describe a separator between each record,the <SeparatorTemplate>element can be used.
The example below inserts a horizontal line between each table row:
<%@ 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>