Aspnet Arraylist
## ASP.NET Web Forms - ArrayList Object
* * *
ArrayList object is a collection of items containing single data values.
* * *
 method.
The following code creates an ArrayList object named mycountries and adds four items:
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
end if
end sub
By default, an ArrayList object contains 16 entries. The ArrayList can be adjusted to its final size using the TrimToSize() method:
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
end if
end sub
By applying the Sort() method, the ArrayList can be organized alphabetically or numerically:
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
end if
end sub
To achieve reverse ordering, the Reverse() method is utilized following the Sort() method:
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
mycountries.Reverse()
end if
end sub
* * *
## Data Binding to ArrayList
The ArrayList object automatically generates text and values for the following controls:
* asp:RadioButtonList
* asp:CheckBoxList
* asp:DropDownList
* asp:Listbox
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 to create the list, and bind the values in the list to the RadioButtonList control:
## Example
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
rb.DataSource=mycountries
rb.DataBind()
end if
end sub
[Show Example Β»](
The DataSource property of the RadioButtonList control is set to the 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 Values that are different from Text, use a Hashtable object or a SortedList object.
YouTip