ArrayList 对象是包含单一数据值的项目的集合。
创建 ArrayList
通过 Add() 方法向 ArrayList 添加项目。
下面的代码创建了一个新的 ArrayList 对象,名为 mycountries,并添加了四个项目:
<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>
默认地,一个 ArrayList 对象包含 16 个条目。可通过 TrimToSize() 方法把 ArrayList 调整为最终尺寸:
<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>
通过 Sort() 方法,ArrayList 也能够按照字母顺序或者数字顺序进行排序:
<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>
要实现颠倒的排序,请在 Sort() 方法后应用 Reverse() 方法:
<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>
把数据绑定到 ArrayList
ArrayList 对象可向下面这些控件自动地生成文本和值:
• asp:RadioButtonList
• asp:CheckBoxList
• asp:DropDownList
• asp:Listbox
如需把数据绑定到一个 RadioButtonList 控件,首先请在一个 .aspx 页面中创建 RadioButtonList 控件(请注意,没有任何 asp:ListItem 元素):
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>