Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to bind Repeater Control to Generic List in ASP.NET

Author: Rudy Pinson
Download Source Code : 1299_BindRepeaterToList.zip

I will show you in this article that how you can bind a Repeater Control to a Generic list in ASP.NET.

 

In one of my previous article on DevASP.NET, I have explained about Repeater Control and show you how you can display data using Repeater Control. Below example will show Generic List binding to repeater control. Source code is available to download. 
 
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Write code below in your aspx page.

    <table>
        <tr>
          <td>
             <asp:Label ID="Label1" runat="server" Text="Customer ID" Font-Bold="True"></asp:Label>
          </td>
          <td>
             <asp:Label ID="Label2" runat="server" Text="Company Name" Font-Bold="True"></asp:Label>
          </td>
          <td>
             <asp:Label ID="Label3" runat="server" Text="Contact Name" Font-Bold="True"></asp:Label>
          </td>
          <td>
             <asp:Label ID="Label4" runat="server" Text="Country" Font-Bold="True"></asp:Label>
          </td>
        </tr>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <tr>
                   <td>
                      <asp:Label ID="lblCustomerID" runat="server"></asp:Label>
                   </td>
                   <td>
                       <asp:Label ID="LblCompanyName" runat="server"></asp:Label>
                   </td>
                   <td>
                       <asp:Label ID="lblContactName" runat="server"></asp:Label>
                   </td>
                   <td>
                       <asp:Label ID="lblCountry" runat="server"></asp:Label>
                   </td>
                 </tr>
             </ItemTemplate>
        </asp:Repeater>
     </table>
     
  4. Now add a Class to your Web Site and write code below in class.

    C#
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
     
    public class Customers
    {
     
        private string _customerID = "";
        private string _companyName = "";
        private string _country = "";
        private string _contactName = "";
       
        public string CustomerID
        {
            set
            {
                this._customerID = value;
            }
            get
            {
                return this._customerID;
            }
        }
        public string ContactName
        {
            set
            {
                this._contactName = value;
            }
            get
            {
                return this._contactName;
            }
        }
        public string CompanyName
        {
            set
            {
                this._companyName = value;
            }
            get
            {
                return this._companyName;
            }
        }
        public string Country
        {
            set
            {
                this._country = value;
            }
            get
            {
                return this._country;
            }
        }
     
    }
     
    VB.NET
     
    Imports Microsoft.VisualBasic
     
    Public Class Customers
        Private _customerID As String = ""
        Private _companyName As String = ""
        Private _country As String = ""
        Private _contactName As String = ""
     
        Public Property CustomerID() As String
            Get
                Return Me._customerID
            End Get
            Set(ByVal value As String)
                Me._customerID = value
            End Set
        End Property
        Public Property ContactName() As String
            Get
                Return Me._contactName
            End Get
            Set(ByVal value As String)
                Me._contactName = value
            End Set
        End Property
        Public Property CompanyName() As String
            Get
                Return Me._companyName
            End Get
            Set(ByVal value As String)
                Me._companyName = value
            End Set
        End Property
        Public Property Country() As String
            Get
                Return Me._country
            End Get
            Set(ByVal value As String)
                Me._country = value
            End Set
        End Property
    End Class
     
  5. Write code below in code behind file of your page. Include “System.Data” and “System.Data.SqlClient” namespace.

    C#
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
     
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.DataSource = GetCustomerDetail();
            Repeater1.DataBind();
        }
     
        private DataTable Customers()
        {
            string query = "SELECT * FROM Customers";
            SqlConnection sqlConnection = new SqlConnection("Data Source=Your data source;Initial Catalog=NORTHWND;Integrated Security=True");
            SqlCommand command = new SqlCommand(query, sqlConnection);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
            DataTable table = new DataTable();
            dataAdapter.Fill(table);
            sqlConnection.Close();
            return table;
        }
     
     
        private List GetCustomerDetail()
        {
            DataTable table = Customers();
     
            List list = new List();
     
            foreach (DataRow dr in table.Rows)
            {
                Customers detail = new Customers();
                detail.CustomerID = dr["CustomerID"].ToString();
                detail.CompanyName = dr["CompanyName"].ToString();
                detail.ContactName = dr["ContactName"].ToString();
                detail.Country = dr["Country"].ToString();
                list.Add(detail);
            }
            return list;
        }
     
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((Label)e.Item.FindControl("lblCustomerID")).Text = ((Customers)e.Item.DataItem).CustomerID;
                ((Label)e.Item.FindControl("lblCompanyName")).Text = ((Customers)e.Item.DataItem).CompanyName;
                ((Label)e.Item.FindControl("lblContactName")).Text = ((Customers)e.Item.DataItem).ContactName;
                ((Label)e.Item.FindControl("lblCountry")).Text = ((Customers)e.Item.DataItem).Country;
              
            }
        }
     
    }
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Repeater1.DataSource = GetCustomerDetail()
            Repeater1.DataBind()
        End Sub
     
        Private Function Customers() As DataTable
            Dim query As String = "SELECT * FROM Customers"
            Dim sqlConnection As New SqlConnection("Data Source=Your Data source;Initial Catalog=NORTHWND;Integrated Security=True")
            Dim command As New SqlCommand(query, sqlConnection)
            Dim dataAdapter As New SqlDataAdapter(command)
            Dim table As New DataTable()
            dataAdapter.Fill(table)
            sqlConnection.Close()
            Return table
        End Function
     
     
        Private Function GetCustomerDetail() As List(Of Customers)
            Dim table As DataTable = Customers()
     
            Dim list As New List(Of Customers)()
     
            For Each dr As DataRow In table.Rows
                Dim detail As New Customers()
                detail.CustomerID = dr("CustomerID").ToString()
                detail.CompanyName = dr("CompanyName").ToString()
                detail.ContactName = dr("ContactName").ToString()
                detail.Country = dr("Country").ToString()
                list.Add(detail)
            Next
            Return list
        End Function
     
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                DirectCast(e.Item.FindControl("lblCustomerID"), Label).Text = DirectCast(e.Item.DataItem, Customers).CustomerID
                DirectCast(e.Item.FindControl("lblCompanyName"), Label).Text = DirectCast(e.Item.DataItem, Customers).CompanyName
                DirectCast(e.Item.FindControl("lblContactName"), Label).Text = DirectCast(e.Item.DataItem, Customers).ContactName
     
                DirectCast(e.Item.FindControl("lblCountry"), Label).Text = DirectCast(e.Item.DataItem, Customers).Country
            End If
        End Sub
    End Class
     
  6. View the Web Site in browser and see the result
 

 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< LINQ to DataSet in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net