ASP.NET - ArrayList object

ArrayList objects are collections of items containing a single data value.

Instance

ArrayList DropDownList

ArrayList RadioButtonList

Create ArrayList

ArrayList objects are collections of items containing a single data value.

Items are added to the ArrayList through the Add() method.

The following code creates a new ArrayList object named mycountries and adds four items:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
end if
end sub
</script>

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

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
end if
end sub
</script>

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

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
end if
end sub
</script>

To achieve reversed sorting, 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("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>

Bind data to ArrayList

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

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

If you need to bind data to a RadioButtonList control, first create a RadioButtonList control in a .aspx page (note that there are no asp:ListItem elements):

<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

Then add the script to build the list and bind 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("China")
  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>

Display this 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:Data values are used as the Text and Value properties of the control. To add a Value different from Text, you can use the Hashtable object or the SortedList object.