Play-Asia.com - Buy Video Games for Consoles and PC - From Japan, Korea and other Regions!

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:

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

 
 
Visitor Review Write a review
No visitor review for this page yet
Write a review
Rating:
Text:
Name:
Email:
(optional)
 
Code:
 

Search:
Shopping Store Online
Clothes (25)
Gift shop (69)
Hand-made (27)
Luxuries (47)
Others (42)
Programming Tutorial
ASP (14)
CSharp
Binding Data to List Control
Data Access with ADO.NET
DataGrid Control
The ArrayList class
The BitArray Class
The Calendar Class
The Hashtable Class
The HttpCookie Class
The HttpRequest Class
The HttpResponse Class
The Queue Class
The Repeater List Control
The SortedList Class
The SqlCommand Class
The SqlConnection Class
The SqlDataReader Class
The SqlParameter Class
The SqlParameterCollection Class
The SqlTransaction Class
HTML (10)
JavaScript (6)
Photoshop (9)
PHP (11)
Entertainment
Anime (6)
E-Cards (12)
Games (20)
Music (7)