阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

ASP.NET Web Forms – ArrayList 对象简介

30次阅读
没有评论

共计 2179 个字符,预计需要花费 6 分钟才能阅读完成。

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

ASP.NET Web Forms – ArrayList 对象简介

创建 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")   
mycou
ntries.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 对象可为下列的控件自动生成文本和值:

  1. asp:RadioButtonList
  2. asp:CheckBoxList
  3. asp:DropDownList
  4. 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 对象。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2179字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中