C# - The Repeater List Control

The most basic list control is the Repeater control. As its name suggests, the Repeater control is used to display a collection of items in a repeating list. The items are rendered in the list based on templates.

Repeater Control Templates

Template Description
HeaderTemplate Defines the content of the list's header
ItemTemplate Defines the layout of items in the list
AlternatingItemTemplate Defines the layout of every other item in the list
SeparatorTemplate Defines the content in between items in the list
FooterTemplate Defines the content of the list's footer

At least, the developers must define ItemTemplate to let the Repeater control know how to display the bound data.

Using Repeater Controls

We will show you how the Repeater control work by using XML as data source. Let prepare for the data source:

<Text>Click here to go to Hotmail.com</Text>
<Text>Click here to go to Yahoo.com</Text>
<Text>Click here to go to Google.com</Text>

This source will save as file name 'repeater_data.xml'. Then we will make the source code to use with this data.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>

<script language="c#" runat="server">
void Page_Load(Object src, EventArgs e){
DataSet ds = new DataSet();
FileStream fs = new FileStream(Server.MapPath("repeater_data.xml"), FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fs); ds.ReadXml(reader);
DataView view = new DataView(ds.Tables[0]); MyRepeater.DataSource = view; MyRepeater.DataBind(); } </script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Repeater Example</title>
<link href="../../../style.css" rel="stylesheet" type="text/css">
<p><b>Repeater Example </b></p>
<asp:Repeater ID="MyRepeater" runat="server">
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Link") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>' /> <br>


The output will be show as the following: