Introduction
In this article our main focus is to bind data to data bound controls by using an ArrayList. So first this article explains you how to create an ArrayList, then it explains how to bind data by using array list.
Create an ArrayList
The ArrayList object is a collection of items containing a single data value.
Items are added to the ArrayList with the Add() method.
The following code 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("India")
mycountries.Add("Australia")
mycountries.Add("japan")
mycountries.Add("Russia")
End If
End Sub
</script>
By default, an ArrayList object contains 16 entries. 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("India")
mycountries.Add("Australia")
mycountries.Add("Japan")
mycountries.Add("Russia")
mycountries.TrimToSize()
End If
End Sub
</script>
An ArrayList can also be sorted alphabetically or numerically with the Sort() method:
<script runat="server">
Sub Page_Load()
If Not Page.IsPostBack Then
Dim mycountries = New ArrayList
mycountries.Add("India")
mycountries.Add("Australia")
mycountries.Add("Japan")
mycountries.Add("Russia")
mycountries.TrimToSize()
mycountries.Sort()
End If
End Sub
</script>
To sort in reverse order, apply the Reverse() method after the Sort() method
<script runat="server">
Sub Page_Load()
If Not Page.IsPostBack Then
Dim mycountries = New ArrayList
mycountries.Add("India")
mycountries.Add("Australia")
mycountries.Add("Japan")
mycountries.Add("Russia")
mycountries.TrimToSize()
mycountries.Sort()
mycountries.Reverse()
End If
End Sub
</script>
Data Binding to an ArrayList
An ArrayList object may automatically generate the text and values to the following controls:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control in an .aspx page:
<html>
<body>
<form id="Form2" runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
Then add the script that builds the list and binds the values in the list to the RadioButtonList control:
<script runat="server">
Sub Page_Load()
If Not Page.IsPostBack Then
Dim mycountries = New ArrayList
mycountries.Add("India")
mycountries.Add("Australia")
mycountries.Add("Japan")
mycountries.Add("Russia")
mycountries.TrimToSize()
mycountries.Sort()
rb.DataSource = mycountries
rb.DataBind()
End If
End Sub
</script>
<html>
<body>
<form id="Form2" runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control.