Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET ArrayList

ASP.NET Web Forms - ArrayList Object


The ArrayList object is a collection of items, each containing a single data value.


Try it - Example

ArrayList DropDownList

ArrayList RadioButtonList


Creating an ArrayList

The ArrayList object is a collection of items, each containing a single data value.

Add items to an ArrayList using the Add() method.

The code below creates an ArrayList object named mycountries and adds four items:

By default, an ArrayList object contains 16 entries. You can adjust the ArrayList to its final size using the TrimToSize() method:

Through the Sort() method, an ArrayList can also be sorted in alphabetical or numerical order:

To sort in reverse order, apply the Reverse() method after the Sort() method:


Binding Data to an ArrayList

The ArrayList object can automatically generate text and values for the following controls:

To bind data to a RadioButtonList control, first create a RadioButtonList control in the .aspx page (without any asp:ListItem elements):

Then add the script that creates the list and binds the values in the list to the RadioButtonList control:

Example

Demo Example »

The DataSource property of the RadioButtonList control is set to this ArrayList, which defines the data source for the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the RadioButtonList control to the data source.

**Note:** The data values are used as the Text and Value properties of the control. To add a Value that differs from the Text, use a Hashtable object or a SortedList object.