当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET入门教程:ArrayList对象

ASP.NET
GridView添加删除按钮终极办法
AjaxPro让.NET的AjaxPro变得简单
c# 实现Word联接Excel的MailMerge功能
解开Ajax技术中的达芬奇密码
专家讲解用.NET编写串口程序的一点心得
利用AJAX和ASP.NET实现简单聊天室
如何快速捕获.NET代码中隐藏的BUG
动态网页原理/.net面面观
从N层到.NET详细剖析原理(2)
从N层到.NET详细剖析原理(1)
ASP.NET效率陷阱之——Attributes
在ASP.NET 2.0中建立站点导航层次(5)
在ASP.NET 2.0中建立站点导航层次(4)
在ASP.NET 2.0中建立站点导航层次(3)
在ASP.NET 2.0中建立站点导航层次(2)
在ASP.NET 2.0中建立站点导航层次(1)
动态网站Web开发PHP、ASP还是ASP.NET(2)
动态网站Web开发PHP、ASP还是ASP.NET(1)
让Apache支持ASP.NET-Apache,ASP.NET
.Net下的数据备份和还原

ASP.NET入门教程:ArrayList对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 41 ::
收藏到网摘: n/a

上一文讲解了数据绑定(Data Binding)来完成带有可选项目的列表,这些可选项目来自某个导入的数据源,比如数据库、XML 文件或脚本。

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>

把数据绑定到 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 控件与数据源绑定在一起。本信息代表文章来源网页教学ruanchen.com请大家去www.ruanchen.com浏览!

注释:数据值作为控件的 Text 和 Value 属性来使用。如需添加不同于 Text 的 Value,既可以使用 Hashtable 对象,也可以使用 SortedList 对象。