ASP.NET Web Forms-ArrayList 对象

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

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

php编程基础教程.pptx|php编程培训,php,编程,基础,教程,pptx
php编程基础教程.pptx

历史上的今天:04月30日

热门专题

云南网站建设|云南网站制作,网站建设,云南网站开发,云南网站设计,云南网页设计,云南网站建设公司,云南网站建设
云南网站建设
云南巨榕教育投资集团有限公司|云南巨榕教育投资集团有限公司,巨榕教育集团,巨榕教育
云南巨榕教育投资集团有限公司
小程序开发|微信小程序,小程序开发,小程序,小程序制作,微信小程序开发,小程序公司,小程序开发公司,分销,三级分销系统,分销系统
小程序开发
易捷尔高职单招|易捷尔高职单招,易捷尔高职单招培训,单招分数线,单招录取分数线,高职单招学校分数线
易捷尔高职单招
易捷尔单招|易捷尔单招,易捷尔单招培训,易捷尔单招报名,易捷尔单招考试,易捷尔单招培训学校,易捷尔单招分数
易捷尔单招
APP开发|app开发_app开发公司_app软件开发_专业app开发_云南app开发公司_app定制_原生app开发定制
APP开发
云南综合高中|云南综合高中
云南综合高中
外贸网站建设|外贸网站建设,英文网站制作,英文网站设计,美国主机空间,外贸建站平台,多语言网站制作
外贸网站建设

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部