3 ASP.NET Repeater Control Tips

The ASP.NET Repeater control is a lightweight templated data-bound list control that allows customization of your layout by repeating a specified template for each item displayed in the list. In this article, we will see 3  lesser known ASP.NET Repeater Control tips

Tip 1: Bind an ASP.NET Repeater to List<string>

Let’s first declare a List<String> for ourselves. Create an Employee and EmployeeList Class

public class Employee
public int EmpID { get; set; }
public string EmpName { get; set; }

public class EmployeeList
static EmployeeList()
emp = new List<Employee>();
emp.Add(new Employee() { EmpID = 1, EmpName = “Marshall” });
emp.Add(new Employee() { EmpID = 2, EmpName = “Matt Damon” });
emp.Add(new Employee() { EmpID = 3, EmpName = “Larry King” });
public static List<Employee> emp { get; set; }

We have an EmployeeList exposed  through a get/set property public static List<Employee> emp { get; set; }. This allows us to consume this List<> using a LINQDataSource, which expects the TableName to be a property, in our case ’emp’.

Now declare a Repeater control with two columns to display this data:

<form id=”form1″ runat=”server”>
<asp:Repeater ID=”rptName” runat=”server”>
<%# ((Employee)Container.DataItem).EmpID %>
<%# ((Employee)Container.DataItem).EmpName %>
</table><br />

The <ItemTemplate> defines how items in the Repeater control are displayed. The most important step is to cast the Container.DataItem object to the ‘Employee’ type.

In the code behind Page_Load, bind the Repeater to the List<Employee> as shown below:

protected void Page_Load(object sender, EventArgs e)
rptName.DataSource = EmployeeList.emp;

and there you go. Your Repeater control is bound to a List<string>
Tip 2: Programmatically access a CheckBox inside a Repeater

Let us say you have a CheckBox defined inside a Repeater control

<asp:Repeater id=”rptName” runat=”server”>
<li><asp:checkbox id=”chkbox” runat=”server />
<input type=”checkbox” id=”<%# Container.DataItem %>”/>

To access this checkbox programmatically and print it’s value for each row generated, use this code in the rptName_ItemDataBound() event. Here we are assuming that the ‘married’ property is been bound to the CheckBox.

foreach (RepeaterItem item in rptName.Items)
CheckBox cb = item.FindControl(“chkbox”) as CheckBox;
if (cb.Checked)
Response.Write(“Status: ” + cb.Attributes[“married”].ToString() + “< br />”);

Tip 3: Binding a DropDownList control inside a Repeater

I have usually found developers binding the DropDownList inside the Repeater_ItemDataBound property of the Repeater Control. Ideally control databinding must be done at the Control level, rather than the Parent level. Here’s how

Let’s assume the following dropdownlist control is defined in the ItemTemplate of the Repeater

<asp:DropDownList ID=”ddlRating” runat=”server”
AutoPostBack=”true” EnableViewState=”false”>

To populate this control, add an OnDataBinding event as shown below

<asp:DropDownList ID=”ddlCities” runat=”server”
AutoPostBack=”true” EnableViewState=”false”

and code the DataBinding event

protected void ddlCities_DataBinding(object sender, System.EventArgs e)
// populate DDL control here


Theo devcury