ASP.NET Web Forms - ArrayList object
This section introduces how to create an ASP.NETArrayList object and describes how to bind data to an ArrayList object.
An ArrayList object is a collection of items that contains a single data value.
ArrayList DropDownList
ArrayList RadioButtonList
An ArrayList object is a collection of items that contains a single data value.
Add items to the ArrayList through the Add() method.
The following code creates an ArrayList object named mycountries and adds four items:
<script runat="server">Sub Page_Loadif Not Page.IsPostBack thendim mycountries=New ArrayListmycountries.Add("Norway")mycountries.Add("Sweden")mycountries.Add("France")mycountries.Add("Italy") end ifend sub</script>
By default, an ArrayList object contains 16 entries. The ArrayList can be adjusted to its final size through the TrimToSize() method:
<script runat="server">Sub Page_Loadif Not Page.IsPostBack thendim mycountries=New ArrayListmycountries.Add("Norway")mycountries.Add("Sweden")mycountries.Add("France")mycountries.Add("Italy") mycountries.TrimToSize()end ifend sub</script>
Through the Sort() method, ArrayList can also be sorted alphabetically or numerically:
<script runat="server">Sub Page_Loadif Not Page.IsPostBack thendim mycountries=New ArrayListmycountries.Add("Norway")mycountries.Add("Sweden")mycountries.Add("France")mycountries.Add("Italy") mycountries.TrimToSize()mycountries.Sort()end ifend sub</script>
To achieve reverse sorting, apply the Reverse() method after the Sort() method:
<script runat="server">Sub Page_Loadif Not Page.IsPostBack thendim mycountries=New ArrayListmycountries.Add("Norway")mycountries.Add("Sweden")mycountries.Add("France")mycountries.Add("Italy") mycountries.TrimToSize()mycountries.Sort()mycountries.Reverse()end ifend sub</script>
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 in the .aspx page (without any asp:ListItem elements):
<html><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:
<script runat="server">Sub Page_Loadif Not Page.IsPostBack thendim mycountries=New ArrayListmycountries.Add("Norway")mycountries.Add("Sweden")mycountries.Add("France")mycountries.Add("Italy")mycountries.TrimToSize()mycountries.Sort()rb.DataSource=mycountriesrb.DataBind( )end ifend sub</script><html><body><form runat="server"><asp:RadioButtonList id="rb" runat="server" /></form></body></html>
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. If you need to add a Value different from Text, please use a Hashtable object or a SortedList object.
The above is the use of ASP.NETArrayList object.