?
This document uses PHP Chinese website manual Release
ArrayList 對(duì)象是包含單個(gè)數(shù)據(jù)值的項(xiàng)目的集合。
ArrayList DropDownList
<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() ???dd.DataSource=mycountries ???dd.DataBind() end?if end?sub sub?displayMessage(s?as?Object,e?As?EventArgs) lbl1.text="Your?favorite?country?is:?"?&?dd.SelectedItem.Text end?sub </script> <!DOCTYPE?html> <html> <body> <form?runat="server"> <asp:DropDownList?id="dd"?runat="server" AutoPostBack="True"?onSelectedIndexChanged="displayMessage"?/> <p><asp:label?id="lbl1"?runat="server"?/></p> </form> </body> </html>
ArrayList 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 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>
ArrayList 對(duì)象是包含單個(gè)數(shù)據(jù)值的項(xiàng)目的集合。
通過(guò) Add() 方法向 ArrayList 添加項(xiàng)目。
下面的代碼創(chuàng)建了一個(gè)名為 mycountries 的 ArrayList 對(duì)象,并添加了四個(gè)項(xiàng)目:
<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>
在默認(rèn)情況下,一個(gè) ArrayList 對(duì)象包含 16 個(gè)條目??赏ㄟ^(guò) TrimToSize() 方法把 ArrayList 調(diào)整為最終尺寸:
<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>
通過(guò) Sort() 方法,ArrayList 也能夠按照字母順序或者數(shù)字順序進(jìn)行排序:
<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>
要實(shí)現(xiàn)反向排序,請(qǐng)?jiān)?Sort() 方法后應(yīng)用 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>
ArrayList 對(duì)象可為下列的控件自動(dòng)生成文本和值:
為了綁定數(shù)據(jù)到 RadioButtonList 控件,首先要在 .aspx 頁(yè)面中創(chuàng)建一個(gè) RadioButtonList 控件(不帶任何 asp:ListItem 元素):
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>
然后添加創(chuàng)建列表的腳本,并且綁定列表中的值到 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>
RadioButtonList 控件的 DataSource 屬性被設(shè)置為該 ArrayList,它定義了這個(gè) RadioButtonList 控件的數(shù)據(jù)源。RadioButtonList 控件的 DataBind() 方法把 RadioButtonList 控件與數(shù)據(jù)源綁定在一起。
注釋:數(shù)據(jù)值作為控件的 Text 和 Value 屬性來(lái)使用。如需添加不同于 Text 的 Value,請(qǐng)使用 Hashtable 對(duì)象或者 SortedList 對(duì)象。