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.
- 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
- ArrayList arrList = new ArrayList();
- arrList.Add(1);
- arrList.Add("System.Datetime.Now");
- arrList.Add("Nikunj Satasiya");
ArrayList stores all the information as objects
Example
- int employeeId= Convert.ToInt32(arrList[0]);
- string employeename = arrList[1].ToString();
- 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
- List<int> GenericsList = new List<int>();
-
- GenericsList.Add(1);
- GenericsList.Add(2);
- GenericsList.Add(3);
-
- for (int i = 0; i < GenericsList.Count; i++)
- {
- Console.WriteLine("My List is : {0}", GenericsList[i]);
- }
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.
C# Code Markup
Before beginning to make C# Code you should feature the following Namespace to access array assortment
- 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.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
-
- string[] arrNames = new string[4];
- arrNames[0] = "Nikunj Satasiya";
- arrNames[1] = "Hiren Dobariya";
- arrNames[2] = "Vivek Ghadiya";
- arrNames[3] = "Pratik Pansuriya";
- Repeater1.DataSource = arrNames;
- Repeater1.DataBind();
-
-
- ArrayList alstNames = new ArrayList();
- alstNames.Add("Nikunj Satasiya");
- alstNames.Add("Hiren Dobariya");
- alstNames.Add("Vivek Ghadiya");
- alstNames.Add("Pratik Pansuriya");
- Repeater2.DataSource = alstNames;
- Repeater2.DataBind();
-
-
- List<string> lstNames = new List<string>();
- lstNames.Add("Nikunj Satasiya");
- lstNames.Add("Hiren Dobariya");
- lstNames.Add("Vivek Ghadiya");
- lstNames.Add("Pratik Pansuriya");
- Repeater3.DataSource = lstNames;
- Repeater3.DataBind();
- }
- }
Summary
When you run this project and load the ASPX page, the values are bound in Repeater control.