Bind an ASP.NET Repeater to a Generic List<>
I recently got a mail from a dotnetcurry reader who wanted to know how to bind a Repeater control to a List<>. In this article we will see how to do so.
A Repeater control is a light weight templated data-bound list. It has no built-in selection, editing, layout or formatting capabilities. Thus if you need capabilities like paging, you have to explicitly build it for your applications.
Let us get started. Open Visual Studio 2008 and choose File > New > Web > ASP.NET Web Application. Type the name as ‘RepeaterBindToList’. Choose the desired location and language (C# or Visual Basic)
Let us first create a List<>. Right click on the project > Add New Item > Choose ‘Class’ from the Templates and type the name as ‘EmployeeList’ and click OK. A dialog appears as shown below:
Click on Yes to save the file in the App_Code folder.
Add the following code to the EmployeeList class file
C#
using System.Collections.Generic;
public class EmployeeList
{
static EmployeeList()
{
emp = new List<Employee>();
emp.Add(new Employee() { EmpID = 1, DeptID = 1, EmpName = "Jack Nolas" });
emp.Add(new Employee() { EmpID = 2, DeptID = 4, EmpName = "Mark Pine" });
emp.Add(new Employee() { EmpID = 3, DeptID = 3, EmpName = "Sandra Simte" });
emp.Add(new Employee() { EmpID = 4, DeptID = 4, EmpName = "Larry Lo" });
emp.Add(new Employee() { EmpID = 5, DeptID = 3, EmpName = "Sudhir Panj" });
emp.Add(new Employee() { EmpID = 6, DeptID = 2, EmpName = "Kathy K" });
emp.Add(new Employee() { EmpID = 7, DeptID = 1, EmpName = "Kaff Joe" });
emp.Add(new Employee() { EmpID = 8, DeptID = 1, EmpName = "Su Lie" });
}
public static List<Employee> emp { get; set; }
}
public class Employee
{
public int EmpID { get; set; }
public int DeptID { get; set; }
public string EmpName { get; set; }
}
VB.NET
Imports Microsoft.VisualBasic
Public Class EmployeeList
Shared Sub New()
emp = New List(Of Employee)()
emp.Add(New Employee() With {.EmpID = 1, .DeptID = 1, .EmpName = "Jack Nolas"})
emp.Add(New Employee() With {.EmpID = 2, .DeptID = 4, .EmpName = "Mark Pine"})
emp.Add(New Employee() With {.EmpID = 3, .DeptID = 3, .EmpName = "Sandra Simte"})
emp.Add(New Employee() With {.EmpID = 4, .DeptID = 4, .EmpName = "Larry Lo"})
emp.Add(New Employee() With {.EmpID = 5, .DeptID = 3, .EmpName = "Sudhir Panj"})
emp.Add(New Employee() With {.EmpID = 6, .DeptID = 2, .EmpName = "Kathy K"})
emp.Add(New Employee() With {.EmpID = 7, .DeptID = 1, .EmpName = "Kaff Joe"})
emp.Add(New Employee() With {.EmpID = 8, .DeptID = 1, .EmpName = "Su Lie"})
End Sub
Private Shared privateemp As List(Of Employee)
Public Shared Property emp() As List(Of Employee)
Get
Return privateemp
End Get
Set(ByVal value As List(Of Employee))
privateemp = value
End Set
End Property
End Class
Public Class Employee
Private privateEmpID As Integer
Public Property EmpID() As Integer
Get
Return privateEmpID
End Get
Set(ByVal value As Integer)
privateEmpID = value
End Set
End Property
Private privateDeptID As Integer
Public Property DeptID() As Integer
Get
Return privateDeptID
End Get
Set(ByVal value As Integer)
privateDeptID = value
End Set
End Property
Private privateEmpName As String
Public Property EmpName() As String
Get
Return privateEmpName
End Get
Set(ByVal value As String)
privateEmpName = value
End Set
End Property
End Class
Note: As you can see in the code above, we have created an EmployeeList and exposed it through a get/set property public static List<Employee> emp { get; set; }. An additional advantage of using the property is that you can now also consume this List<> using a LINQDataSource, which expects the TableName to be a property, in our case 'emp'. So LINQDataSource can also consume a List<> if exposed via a property.
Now return to your page and add a repeater control with a header, item and footer template as shown below
C#
<asp:Repeater ID="rptName" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>EmployeeID</th>
<th>DepartmentID</th>
<th>EmployeeName</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# ((Employee)Container.DataItem).EmpID %>
</td>
<td>
<%# ((Employee)Container.DataItem).DeptID %>
</td>
<td>
<%# ((Employee)Container.DataItem).EmpName %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table><br />
</FooterTemplate>
</asp:Repeater>
VB.NET
<asp:Repeater ID="rptName" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>EmployeeID</th>
<th>DepartmentID</th>
<th>EmployeeName</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#DirectCast(Container.DataItem, Employee).EmpID%>
</td>
<td>
<%#DirectCast(Container.DataItem, Employee).DeptID%>
</td>
<td>
<%#DirectCast(Container.DataItem, Employee).EmpName%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table><br />
</FooterTemplate>
</asp:Repeater>
The <ItemTemplate> defines how items in the Repeater control are displayed. Also observe how we have cast the Container.DataItem object to the ‘Employee’ type. This step is very essential.
Now one final step is to bind the Repeater to the List<Employee> as shown below:
C#
protected void Page_Load(object sender, EventArgs e)
{
rptName.DataSource = EmployeeList.emp;
rptName.DataBind();
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
rptName.DataSource = EmployeeList.emp
rptName.DataBind()
End Sub
Run the application and you can see your Repeater bound to the List<Employee>
I hope this article was useful and I thank you for viewing it. The entire source code of this article can be downloaded from here.