WF References

ASP.NET Web Forms - The Repeater Control

The Repeater control is used to display a repeated list of items that are bound to the control.

Bind a DataSet to a Repeater Control

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database
table, an XML file, or another list of items. Here we will show how to bind an XML file to a Repeater control.

First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the
following directive at the top of an .aspx page:

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

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:

<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. 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:

<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 the script that creates the DataSet and binds the mycdcatalog
DataSet to the Repeater control. We also fill the Repeater control with HTML
tags and bind the data items to the cells in the<ItemTemplate> section with the
<%#Container.DataItem("fieldname")%> method:

Example

<%@ 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>

Using the <AlternatingItemTemplate>

You can add an <AlternatingItemTemplate> element after the <ItemTemplate>
element to describe the appearance of alternating rows of output. In the
following example each other row in the table will be displayed in a light grey
color:

Example

<%@ 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>

Web Certificates

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy.
Copyright 1999-2015 by Refsnes Data. All Rights Reserved.