ASP.NET Web Forms - ArrayList Object
An ArrayList object is a collection of items that contain single data values.
Try it Yourself - Example
ArrayList DropDownList
ArrayList RadioButtonList
Creating an ArrayList
An ArrayList object is a collection of items that contain single data values.
Add items to the ArrayList by using the Add() 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
</script>
By default, an ArrayList object contains 16 entries. The ArrayList can be adjusted to its final size by 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
</script>
Through the Sort() method, the ArrayList can also be sorted in alphabetical or numerical order:
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>
To achieve reverse sorting, apply the Reverse() method after 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
</script>
Binding Data to an ArrayList
An ArrayList object can automatically generate text and values for the following controls:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
To bind data to the RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in the .aspx page:
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
Then add the script that creates 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
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
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 a Value that differs from the Text, use a Hashtable object or a SortedList object.
Other Extensions