A collection of items containing a single data value is called the ArrayList object.
A D V E R T I S E M E N T
Create an ArrayList
A collection of items containing a single data value is called the ArrayList object.
With the Add() method,items are added to the ArrayList .
The code below creates a new ArrayList object named mycountries and four items are added:
<script runat="server">
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
</script>
By default, an ArrayList object contains 16 entries and an ArrayList can be sized to its final size with the TrimToSize() method:
<script runat="server">
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
</script>
An ArrayList can also be sorted numerically or alphabetically with the Sort() method:
<script runat="server">
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
</script>
Apply the Reverse() method after the Sort() method to sort in reverse order,
<script runat="server">
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
</script>
Data Binding to an ArrayList
An ArrayList object may automatically generate the values and text to the following controls:
asp:RadioButtonList
asp:DropDownList
asp:CheckBoxList
asp:Listbox
First create a RadioButtonList control (without any asp:ListItem elements) in
an .aspx page to bind data to a RadioButtonList control:
Then add the script that binds the values and builds the list in the list to the RadioButtonList control:
<script runat="server">
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
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
The DataSource property of the RadioButtonList control is used to set to the ArrayList and it defines the data source of the RadioButtonList control and the DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control.
Note: The data values are used as both the Text and Value properties for the control and to add Values that are different from the Text, use either the Hashtable object or the SortedList object.