Web 表單 ArrayList
ASP.NET Web Forms - ArrayList 物件
#ArrayList 物件是包含單一資料值的項目的集合。
試試看- 實例
ArrayList DropDownList
ArrayList RadioButtonList
「建立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>
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")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
end if
end sub
</script>
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>
<script runat="server">
要實作反向排序,請在Sort() 方法後面套用Reverse() 方法: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>
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>
<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>
#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 物件可為下列的控制項自動產生文字與值:
- ##asp:RadioButtonList #asp:CheckBoxListasp:DropDownListasp:Listbox
<body>
<form runat="server">
< ;asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
#然後新增建立清單的腳本,並且綁定清單中的值到RadioButtonList 控制項:
#實例<form runat="server">
< ;asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
<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
sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
##執行實例»
點擊"運行實例" 按鈕查看線上實例
RadioButtonList 控制項的DataSource 屬性被設定為該ArrayList,它定義了這個RadioButtonList 控制項的資料來源。 RadioButtonList 控制項的 DataBind() 方法把 RadioButtonList 控制項與資料來源綁在一起。
註解:
資料值作為控制項的 Text 和 Value 屬性來使用。如需新增不同於 Text 的 Value,請使用 Hashtable 物件或 SortedList 物件。