ASP.NET Web Forms 自学篇:(十) ArrayList 对象

ASP.NET Web Forms - ArrayList 对象


下一篇:ASP.NET Web Forms 自学篇:(十一) Hashtable 对象

上一篇:ASP.NET Web Forms 自学篇:(九) 数据绑定


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


尝试一下 - 实例

ArrayList DropDownList

<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()
   dd.DataSource=mycountries
   dd.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>

<!DOCTYPE html>
<html>
<body>

<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

运行结果:
在这里插入图片描述
ArrayList 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

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>

<!DOCTYPE html>
<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

运行结果:
在这里插入图片描述


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


下一篇:ASP.NET Web Forms 自学篇:(十一) Hashtable 对象

上一篇:ASP.NET Web Forms 自学篇:(九) 数据绑定


友情链接:

ASP.NET MVC零基础自学篇:(一)ASP.NET MVC简介

ASP.NET Razor篇——标记

C#将Excel表格内容导入数据库–ASP.NET MVC

JQuery ajax 实现关闭弹窗,刷新列表

ASP.NET MVC的 Action跳转方法归纳部分

jQuery实现checkbox多选,单选与数据库交互传值,赋值整理

C# String字符串方法的使用归纳(直观,粗暴)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值