ArrayList 对象是包含单个数据值的项目的集合。
通过 Add() 方法向 ArrayList 添加项目。
下面的代码创建了一个名为 mycountries 的 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") |
end if |
end sub |
</script> |
<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> |
<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> |
<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> |
ArrayList 对象可为下列的控件自动生成文本和值:
为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素):
<html> |
<body> |
<form runat="server"> |
<asp:RadioButtonList id="rb" runat="server" /> |
</form> |
</body> |
</html> |
<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> |
RadioButtonList 控件的 DataSource 属性被设置为该 ArrayList,它定义了这个 RadioButtonList 控件的数据源。RadioButtonList 控件的 DataBind() 方法把 RadioButtonList 控件与数据源绑定在一起。
注释:
数据值作为控件的 Text 和 Value 属性来使用。如需添加不同于 Text 的 Value,请使用 Hashtable 对象或者 SortedList 对象。