ASP.NET SortedList
asp.net web forms - sortedlist 對象
sortedlist 對象結(jié)合了 arraylist 對象和 hashtable 對象的特性。

嘗試一下 - 實例
sortedlist 對象
sortedlist 對象包含用鍵/值對表示的項目。sortedlist 對象按照字母順序或者數(shù)字順序自動地對項目進(jìn)行排序。
通過 add() 方法向 sortedlist 添加項目。通過 trimtosize() 方法把 sortedlist 調(diào)整為最終尺寸。
下面的代碼創(chuàng)建了一個名為 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>
數(shù)據(jù)綁定
sortedlist 對象可為下列的控件自動生成文本和值:
- asp:radiobuttonlist
- asp:checkboxlist
- asp:dropdownlist
- asp:listbox
為了綁定數(shù)據(jù)到 radiobuttonlist 控件,首先要在 .aspx 頁面中創(chuàng)建一個 radiobuttonlist 控件(不帶任何 asp:listitem 元素):
<html>
<body>
<form runat="server">
<asp:radiobuttonlist id="rb" runat="server" autopostback="true" />
</form>
</body>
</html>
<body>
<form runat="server">
<asp:radiobuttonlist id="rb" runat="server" autopostback="true" />
</form>
</body>
</html>
然后添加創(chuàng)建列表的腳本,并且綁定列表中的值到 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>
</html>
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>
</html>
然后我們添加一個子例程,當(dāng)用戶點(diǎn)擊 radiobuttonlist 控件中的某個項目時,該子例程會被執(zhí)行。當(dāng)某個單選按鈕被點(diǎn)擊時,label 中會出現(xiàn)一行文本:
實例
<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>
<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>
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>
<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>