SortedList 對象兼有 ArrayList 和 Hashtable 對象的特性。
SortedList 對象包含用鍵/值對表示的項目。SortedList 對象可按照字符順序或數字順序自動地對項目進行排序。
通過 Add() 方法向 SortedList 添加項目。SortedList 可通過 TrimToSize() 方法調整為最終尺寸。本信息代表文章來源網頁教學webjx.com請大家去www.webjx.com浏覽!
下面的代碼創建了一個名為 mycountrIEs 的 SortedList,並添加了四個元素:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountrIEs.Add("I","Italy") end if end sub </script>
SortedList 對象可自動地為下面的控件生成文本和值:
如需把數據綁定到 RadioButtonList 控件,首先請在 aspx 文件中創建一個 RadioButtonList 控件(沒有任何 ASP:ListItem 元素):
<html> <body> <form runat="server"> <ASP:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </Html>
然後添加構建列表的腳本:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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>
然後我們添加一個子例程,該子例程會在用戶點擊 RadioButtonList 控件中的項目時執行。當單選按鈕被點擊時,文本將出現在 label 中:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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>