Latest web development tutorials

ASP.NET ArrayList

ArrayList object that contains a collection of individual data value items.


Examples

Try - Example

ArrayList DropDownList

ArrayList RadioButtonList


Create ArrayList

ArrayList object that contains a collection of individual data value items.

Adding items to the ArrayList via Add () method.

The following code creates an 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("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
end if
end sub
</script>

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

<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>

By Sort () method, ArrayList can be in alphabetical order or numerical order Sort by:

<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 achieve reverse sort, after the Sort () Method Reverse () 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 an ArrayList

ArrayList object can automatically generate text and values ​​for 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 (without any asp: ListItem elements):

<html>
<body>

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

</body>
</html>

Then add the script to create the list, and the binding values ​​in the list to the RadioButtonList control:

Examples

<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>

The demonstration >>

RadioButtonList control DataSource property is set to the ArrayList, which defines the data source of the RadioButtonList control. RadioButtonList control's DataBind () method of the RadioButtonList control binds the data source.

Note: The data value as Text and Value property of the control to use.Unlike Text To add the Value, please use the Hashtable object or the SortedList object.