Web 表單排序列表
SortedList 物件結合了 ArrayList 物件和 Hashtable 物件的特性。
試試看- 實例
SortedList RadiobuttonList 1
SortedList RadiobuttonList 2
#SortedList DropDownList
SortedList 物件
SortedList 物件包含以鍵/值對錶示的項目。 SortedList 物件會依照字母順序或數字順序自動地對項目進行排序。
透過 Add() 方法向 SortedList 新增項目。透過 TrimToSize() 方法把 SortedList 調整為最終尺寸。
下面的程式碼建立了一個名為mycountries 的SortedList 對象,並加入了四個元素:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
# mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
# mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
資料綁定
SortedList 物件可為下列的控制項自動產生文字和值:
- ##asp:RadioButtonListasp:CheckBoxListasp:DropDownList
- asp:Listbox
<html>
#########################################################################################################################################################################<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#</body>
</html>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#</body>
</html>
然後新增建立清單的腳本,並且綁定清單中的值到RadioButtonList 控制項:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
# mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub
</script>
<html>
<body>
#<form runat ="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#</body>sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
# mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub
</script>
<html>
<body>
#<form runat ="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#然後我們加入子例程,當使用者點選RadioButtonList 控制項中的某個項目時,該子程式會被執行。當某個單選按鈕被點擊時,label 中會出現一行文字:
#
實例
#
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("N","Norway") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") rb.DataSource=mycountries rb.DataValueField="Key" rb.DataTextField="Value" 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>##執行實例»點擊"運行實例" 按鈕查看線上實例