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

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


3 Responses to "3 ASP.NET Repeater Control Tips"
  1. Anonymous said...
    February 20, 2013 at 2:53 AM

    very nice post.

    Thank You.

  2. Anonymous said...
    April 8, 2013 at 8:32 AM

    Good one.


  3. Anonymous said...
    April 8, 2013 at 10:05 AM

    very nice post.

    But I want to know difference between repeater & gridview and which one is better.



Copyright © 2009-2016 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions