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">
<div>
<asp:Repeater ID="rptName" runat="server">
<HeaderTemplate>
<table>
        <tr>
            <th>EmployeeID</th>
            <th>EmployeeName</th>
    </tr>  
</HeaderTemplate>
    <ItemTemplate>
    <tr>
        <td>
        <%# ((Employee)Container.DataItem).EmpID %>
        </td>
        <td>
        <%# ((Employee)Container.DataItem).EmpName %>
        </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>     
    </table><br />
</FooterTemplate>
</asp:Repeater>
</div>
</form>


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;
    rptName.DataBind();
}


and there you go. Your Repeater control is bound to a List<string>
image
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 %>"/>
</asp:Repeater>


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">
</asp:DropDownList>
</td>


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

<asp:DropDownList ID="ddlCities" runat="server"
    AutoPostBack="true" EnableViewState="false"
    OnDataBinding="ddlCities_DataBinding">
</asp:DropDownList>

and code the DataBinding event

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





About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

3 comments:

Anonymous said...

very nice post.



Thank You.

Anonymous said...

Good one.

Thanks.

Anonymous said...

very nice post.

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

Thanks.