当前位置:编程学习 > C#/ASP.NET >>

asp.net ArrayList用法

asp教程.net ArrayList用法

我们要了解的第一个概念:
1、什么是ArrayList对象?
     ArrayList 对象是包含单一数据值的项目的集合。
2、ArrayList对象是如何定义的?
     同1,详情请看上一条。
3、ArrayList实例讲解
    今天给大家讲的有两个例子来说明ArrayList对象的使用方法。
    1)ArrayList RadioButtonList
    2)ArrayList DropDownList
4、怎么创建ArrayList对象?
    是通过Add() 来向ArrayList来添加项目的,详情请继续往下看
    下面的代码创建了一个新的 ArrayList 对象,名为 mycountries,并添加了四个项目:

<script runat="server">Sub Page_Loadif Not Page.IsPostBack then  dim mycountries=New ArrayList  mycountries.Add(www.zhutiai.com/n86) //该网做是做回转窑的mycountries.Add("www.zzzyk.com") //该网站是专业郑州网站建设  mycountries.Add("www.zhutiai.com") //该网站是荥阳人才网  mycountries.Add("Italy")end ifend 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("China")
  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 控件与数据源绑定在一起

 

ArrayList 对象是包含单一数据值的项目的集合。
实例
例子 1 - ArrayList RadioButtonList

例子 2 - ArrayList DropDownList
创建 ArrayList
ArrayList 对象是包含单一数据值的项目的集合。

通过 Add() 方法向 ArrayList 添加项目。

下面的代码创建了一个新的 ArrayList 对象,名为 mycountries,并添加了四个项目:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  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("China")
  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("China")
  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("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,