当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - The ArrayList Object(数组列表对象)

ASP.NET
ASP.NET 介绍
ASP 和 ASP.NET 之间的区别
asp.net的安装
asp.net的网页
ASP.NET - 服务器控件
ASP.NET - 事件
ASP.NET Web 表单
ASP.NET 维持浏览状态
ASP.NET - TextBox(文本框)控件
ASP.NET - 按钮控件
ASP.NET - Data Binding(数据绑定)
ASP.NET - The ArrayList Object(数组列表对象)
ASP.NET - The Hashtable Object 哈希表对象
ASP.NET - The SortedList Object 可排序列表对象
ASP.NET - XML 文件
ASP.NET - Repeater Control 转发器控件
ASP.NET - The DataList Control 数据列表控件
ASP.NET - Database Connection 数据库连接
ASP.NET 2.0 新特征
ASP.NET 2.0 - Master Pages 控制页[主页]

ASP.NET - The ArrayList Object(数组列表对象)


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

The ArrayList object is a collection of items containing a single data value.
数组列表对象为一组包含单一数据值的集合。


Examples

Example 1 - ArrayList RadioButtonList [数组列表 - 单选按钮列表]

Example 2 - ArrayList DropDownList [ 数组列表 - 下拉列表 ]


Create an ArrayList
建立一个数组列表

The ArrayList object is a collection of items containing a single data value.
数组列表对象为一组包含单一数据值的集合。

Items are added to the ArrayList with the Add() method.
使用数组列表中的Add()方法可加入内容。

The following code creates a new ArrayList object named mycountries and four items are added:
以下代码将会建立一个名为mycountries的数组列表并填加进四项内容:

<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>

By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize() method:
默认情况下,一个数组列表对象会包含16个条目。可以使用TrimToSize()这个方法来确定数组列表它的最终大小:

<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>

An ArrayList can also be sorted alphabetically or numerically with the Sort() method:
数组列表还可以通过Sort()方法来选择是依据字母或是数字来进行排列:

<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>

To sort in reverse order, apply the Reverse() method after the Sort() method:
在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>

 


Data Binding to an ArrayList
数据绑定到数组列表

An ArrayList object may automatically generate the text and values to the following controls:
数组列表可在以下控件中字生成文字以及值:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
要在RadioButtonList(单选按钮列表)控件中绑上数据,首先要在一个.aspx页面中建立一个RadioButtonList控件(不需要任何asp:ListItem 元素)

<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

Then add the script that builds the list and binds the values in the list to the RadioButtonList control:
然后加入脚本给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>

The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control.
RadioButtonList 控件的 DataSource(数据源)在数组列表中进行了设置并且它定义了RadioButtonList控件的数据源。RadioButtonList控件通过使用DataBind()方法将数据源和控件进行绑定

Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object.
注意:数据值即可作为控件的Text(显示文字)也可以是Value(值)。要加入的值与显示文字有点区别,可以使用Hashtable 或是 SortedList 对象。

评论 (0) All

登陆 | 还没注册?