How To Bind Repeater Control Using Array, Arraylist, And Generic List Assortment In ASP.NET

Introduction

This article demonstrates a way to bind Repeater control using Array, ArrayList, and Generic List assortment in ASP.NET using C#.

What is Array?

An array is a fixed-size ordered assortment of components with the same name and same datatype.

What is ArrayList?

An ArrayList is a dynamic arrangement where things are added far away from the array. ArrayList belongs to the System.Collections namespace, thus you would like to import the subsequent namespace.

  1. using System.Collections;  

ArrayList doesn't have sort restriction for storing information. You can store something in ArrayList. Additionally, you'll be able to store multiple kinds of objects in the same ArrayList.

Example

  1. ArrayList arrList = new ArrayList();  
  2. arrList.Add(1); //int value  
  3. arrList.Add("System.Datetime.Now"); //datetime  
  4. arrList.Add("Nikunj Satasiya");// string  

ArrayList stores all the information as objects

Example

  1. int employeeId= Convert.ToInt32(arrList[0]);  
  2. string employeename = arrList[1].ToString();  
  3. DateTime datetime = Convert.ToDateTime(arrList[2]);  

Here, arrList[1] and arrList[2] shows the index/position valuable within the list

Generally, arrayList is especially designed for .NET Framework 2.0 because during that period generic list didn't exist

What is GenericsList?

The syntax to make generic list assortment is similar to the declaration of arrays. You declare the List, populate its members, then access the members.

Generic List is accessible to the System.Collections.Generic namespace, so you would like to first import the subsequent namespace in codebehind.

System.Collections.Generic

In Generic List (List "L"), where L means information sort, like string, int, DateTime and etc.

Example

  1. List<int> GenericsList = new List<int>();  
  2.   
  3.     GenericsList.Add(1);  
  4.     GenericsList.Add(2);  
  5.     GenericsList.Add(3);  
  6.   
  7.     for (int i = 0; i < GenericsList.Count; i++)  
  8.     {  
  9.         Console.WriteLine("My List is : {0}", GenericsList[i]);  
  10.     }  

Generic List stores all the information of the kind it's declared. So, obtaining the information back is trouble free and no kind of conversions are needed.

Generic List should be used rather than ArrayList unless a specific demand comes for more than the  .NET 2.0 Framework.

ASPX Code Markup

The ASPX Code Markup consists of an ASP.Net Repeater control rendered as HTML Table.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head id="Head1" runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         body  
  10.         {  
  11.             font-family: Arial;  
  12.             font-size: 10pt;  
  13.         }  
  14.         table  
  15.         {  
  16.             border: 1px solid #ccc;  
  17.             border-collapse: collapse;  
  18.             background-color: #fff;  
  19.         }  
  20.         table th  
  21.         {  
  22.             background-color: #ff6200;  
  23.             color: #ffffff;  
  24.             font-weight: bold;  
  25.         }  
  26.         table th, table td  
  27.         {  
  28.             padding: 5px;  
  29.             border: 1px solid #ff6200;  
  30.         }  
  31.         table, table table td  
  32.         {  
  33.             border: 0px solid #ccc;  
  34.         }  
  35.     </style>  
  36. </head>  
  37. <body>  
  38.     <form id="form1" runat="server">  
  39.         <center>  
  40.     <div>  
  41.     <u>Bind Using Array</u>  
  42.     <br />  
  43.     <br />  
  44.     <asp:Repeater ID="Repeater1" runat="server">  
  45.         <HeaderTemplate>  
  46.             <table cellspacing="0" rules="all" border="1">  
  47.                 <tr>  
  48.                     <th>  
  49.                         Employee Names  
  50.                     </th>  
  51.                 </tr>  
  52.         </HeaderTemplate>  
  53.         <ItemTemplate>  
  54.             <tr>  
  55.                 <td>  
  56.                     <%# Container.DataItem %>  
  57.                 </td>  
  58.             </tr>  
  59.         </ItemTemplate>  
  60.         <FooterTemplate>  
  61.             </table>  
  62.         </FooterTemplate>  
  63.     </asp:Repeater>  
  64.     <hr />  
  65.     <u>Bind Using ArrayList</u>  
  66.     <br />  
  67.     <br />  
  68.     <asp:Repeater ID="Repeater2" runat="server">  
  69.         <HeaderTemplate>  
  70.             <table cellspacing="0" rules="all" border="1">  
  71.                 <tr>  
  72.                     <th>  
  73.                         Employee Names  
  74.                     </th>  
  75.                 </tr>  
  76.         </HeaderTemplate>  
  77.         <ItemTemplate>  
  78.             <tr>  
  79.                 <td>  
  80.                     <%# Container.DataItem %>  
  81.                 </td>  
  82.             </tr>  
  83.         </ItemTemplate>  
  84.         <FooterTemplate>  
  85.             </table>  
  86.         </FooterTemplate>  
  87.     </asp:Repeater>  
  88.     <hr />  
  89.     <u>Bind Using Generic List</u>  
  90.     <br />  
  91.     <br />  
  92.     <asp:Repeater ID="Repeater3" runat="server">  
  93.         <HeaderTemplate>  
  94.             <table cellspacing="0" rules="all" border="1">  
  95.                 <tr>  
  96.                     <th>  
  97.                         Employee Names  
  98.                     </th>  
  99.                 </tr>  
  100.         </HeaderTemplate>  
  101.         <ItemTemplate>  
  102.             <tr>  
  103.                 <td>  
  104.                     <%# Container.DataItem %>  
  105.                 </td>  
  106.             </tr>  
  107.         </ItemTemplate>  
  108.         <FooterTemplate>  
  109.             </table>  
  110.         </FooterTemplate>  
  111.     </asp:Repeater>  
  112.     </div>  
  113.              </center>  
  114.     </form>  
  115. </body>  
  116. </html>  

C# Code Markup

Before beginning to make C# Code you should feature the following Namespace to access array assortment

  1. using System.Collections;  

Now, you'll be able to begin to write down C# Code in Codebehind.

To Bind/Populate Repeater control using Array, Array List and generic-List assortment in ASP.Net using C# you must write the following code in load event of the page. 

  1. protected void Page_Load(object sender, EventArgs e)  
  2.    {  
  3.        if (!this.IsPostBack)  
  4.        {  
  5.            //Bind Repeater using Array of string.  
  6.            string[] arrNames = new string[4];  
  7.            arrNames[0] = "Nikunj Satasiya";  
  8.            arrNames[1] = "Hiren Dobariya";  
  9.            arrNames[2] = "Vivek Ghadiya";  
  10.            arrNames[3] = "Pratik Pansuriya";  
  11.            Repeater1.DataSource = arrNames;  
  12.            Repeater1.DataBind();  
  13.   
  14.            //Bind Repeater using ArrayList.  
  15.            ArrayList alstNames = new ArrayList();  
  16.            alstNames.Add("Nikunj Satasiya");  
  17.            alstNames.Add("Hiren Dobariya");  
  18.            alstNames.Add("Vivek Ghadiya");  
  19.            alstNames.Add("Pratik Pansuriya");  
  20.            Repeater2.DataSource = alstNames;  
  21.            Repeater2.DataBind();  
  22.   
  23.            //Bind Repeater using List of string.  
  24.            List<string> lstNames = new List<string>();  
  25.            lstNames.Add("Nikunj Satasiya");  
  26.            lstNames.Add("Hiren Dobariya");  
  27.            lstNames.Add("Vivek Ghadiya");  
  28.            lstNames.Add("Pratik Pansuriya");  
  29.            Repeater3.DataSource = lstNames;  
  30.            Repeater3.DataBind();  
  31.        }  
  32.    }  

Summary

When you run this project and load the ASPX page, the values are bound in Repeater control.

Output

Ebook Download
View all
Learn
View all