ASP.NET Web Forms
- ArrayList object
This section describes how to create
ASP.NET arrayList object and describes how to bind data to the ArrayList object.
The ArrayList object is a collection of items that contain a single data value.
Try it - instance
ArrayList DropDownList
ArrayList RadioButtonList
Create ArrayList
The ArrayList object is a collection of items that contain a single data value.
Add an item to ArrayList using the Add() method.
The following code creates an ArrayList object called mycountries and adds four items:
<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.
ArrayList can be adjusted to its final size via 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>
With the Sort() method, ArrayList can also be sorted alphabetically or numerically:
<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>
To reverse-sort, 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("Norway")
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 automatically generate text and values for the following controls:
-
asp:RadioButtonList
-
asp:CheckBoxList
-
asp:DropDownList
-
asp:Listbox
In order to bind data to the RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in the .aspx page:
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
Then add the script that created 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("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>
Demo Examples . . .
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: Data
values are used as the Text and Value properties of the control.
To add a Value different from Text, use the Hashtable object or the SortedList object.
That's ASP.NET use of arrayList objects.