ASP.NET - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.NET ArrayList

ASP.NET ArrayListT

shape Description

The ASP.NET ArrayList is a collection of items containing a single data value. One can add value to it as much as they wish, because it doesn’t have any predefined size information. By default, ArrayList can contain upto 16 entries. But one can resize it with ‘TrimToSize()’ method. To add new value to ArrayList, use ‘Add()’ method. In the below example, added country names to an ArrayList using ‘Add()’ method. [csharp]ArrayList spArrayList = new ArrayList(); spArrayList.Add("India"); spArrayList.Add("USA"); spArrayList.Add("England"); spArrayList.Add("Australia"); spArrayList.Add("South Africa");[/csharp] ArrayList can be used as data source for Data Binding controls like DropDownList, RadioButton, CheckBoxList etc.

shape Example

In the below example, one can add a ‘button’ and a ‘DropDownList’ in a form. On clicking of the button the DropDownList will be populated. Here, the data source for the DropDownList is an ArrayList. At design page, add two controls button and DropDownList. At ‘Default.aspx.cs’ page, add country names to the ArrayList using ‘Add()’ method [csharp]protected void spButton_Click(object sender, EventArgs e) { ArrayList spArrayList = new ArrayList(); spArrayList.Add("India"); spArrayList.Add("USA"); spArrayList.Add("England"); spArrayList.Add("Australia"); spArrayList.Add("South Africa"); spDropDownList.DataSource = spArrayList; spDropDownList.DataBind(); } [/csharp] When viewed in browser the page will appear like below Here user has to click on the button and then the DropDownList will be populated. One can sort the order of the values using ‘Sort()’ and ‘Reverse()’ and can use these properties as below. [csharp] spArrayList.Sort(); spArrayList.Reverse(); spArrayList.TrimToSize();[/csharp]