Create new account I forgot my password    

Bind an ASP.NET Repeater to a Generic List<>
Rating: 12 user(s) have rated this article Average rating: 3.9
Posted by: Suprotim Agarwal, on 5/25/2009, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 13784 times
Abstract: In this article, I will explain how to bind an ASP.NET Repeater to a List<>

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:
 
Visual_Studio_AppCode
 
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>
 
Repeater_Bound_To_List
 
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.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by David on Sunday, July 26, 2009 7:28 PM
Why cut the code in half on the first page?  Just show it all on one page.
Comment posted by Steven on Monday, July 27, 2009 9:39 AM
Whilst the code accomplishes its task, I would create a protected (or public, if you're so inclined) property that return the Page's GetDateItem, for example:

protected Employee Employee
{
    get
    {
        return GetDataItem() as Employee;
    }
}

And then use the property in the DataBind syntax, for example:

<%# Employee.EmpID %>
Comment posted by Suprotim Agarwal on Tuesday, July 28, 2009 1:53 AM
David: Viewers gave us a feedback that they needed a wizard like interface for viewing article. I think it's the postback that bothers you. We are planning to launch a new CMS and all these issue will be fixed. Hang on and keep visiting us :)

Steven: Thanks for your tip!

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER