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.111cn.net/n86) //該網做是做迴轉窯的mycountries.Add("www.111cn.net") //該網站是專業鄭州網站建設 mycountries.Add("www.111cn.net") //該網站是滎陽人才網 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>