Web Forms ArrayList
ASP.NET Web Forms - ArrayList Object
An ArrayList object is a collection of items that contains a single data value.
Try it - Example
ArrayList DropDownList
ArrayList RadioButtonList
Create ArrayList
An ArrayList object is a collection of items that contains a single data value.
Add items to ArrayList through 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 the final size through 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, 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, please 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>
Bind data to ArrayList
The ArrayList object can 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, you must first create a RadioButtonList control in the .aspx page (without any asp:ListItem elements):
<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
<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 sub displayMessage(s as Object,e As EventArgs) lbl1.text="Your favorite country is: " & rb.SelectedItem.Text end sub </script> <!DOCTYPE html> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" onSelectedIndexChanged="displayMessage" /> <p><asp:label id="lbl1" runat="server" /></p> </form> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
The DataSource property of the RadioButtonList control is set to the ArrayList, which defines the data source of 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, use a Hashtable object or a SortedList object.