Bind an ASP.NET Repeater to a Generic List<>

Posted by: Suprotim Agarwal , on 5/25/2009, in Category ASP.NET
Views: 49967
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and the C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook