Web Forms 教程

浏览16709
更新时间2022-04-11

ArrayList 对象是包含单个数据值的项目的集合。


尝试一下 - 实例

ArrayList DropDownList

ArrayList RadioButtonList


创建 ArrayList

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>

在默认情况下,一个 ArrayList 对象包含 16 个条目。可通过 TrimToSize() 方法把 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>

通过 Sort() 方法,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()
   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("Norway")
   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>

然后添加创建列表的脚本,并且绑定列表中的值到 RadioButtonList 控件:

实例

<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 对象。


相关视频

更多

免费

php8,我来也
初级php8,我来也

34.2万次学习

收藏

免费

Thinkphp6.0正式版视频教程
中级Thinkphp6.0正式版视频教程

39.8万次学习

收藏

免费

细说PHP第一季
中级细说PHP第一季

31.3万次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

65.1万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

41.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

76.8万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.2万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.4万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.3万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

133.6万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载