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:
<DocumentElement>
<Links>
<Text>Click here to go to Hotmail.com</Text>
<Link>http://www.hotmail.com/</Link>
</Links>
<Links>
<Text>Click here to go to Yahoo.com</Text>
<Link>http://www.yahoo.com/</Link>
</Links>
<Links>
<Text>Click here to go to Google.com</Text>
<Link>http://www.google.com/</Link>
</Links>
</DocumentElement>
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);
fs.Close();
DataView view = new DataView(ds.Tables[0]); MyRepeater.DataSource = view; MyRepeater.DataBind(); } </script><html>
<head>
<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">
</head>
<body>
<p><b>Repeater Example </b></p>
<hr>
<asp:Repeater ID="MyRepeater" runat="server">
<itemtemplate>
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Link") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>' /> <br>
</itemtemplate>
</asp:Repeater>
</body>
</html>
Output
The output will be show as the following: