Hashtable 對象包含用鍵/值對表示的項目。
例子 1 - Hashtable RadioButtonList
例子 2 - Hashtable RadiobuttonList
例子 3 - Hashtable DropDownList
Hashtable 對象包含用鍵/值對表示的項目。鍵被用作索引,通過搜索其鍵,可以實現對值的快速搜索。
通過 Add() 方法向 Hashtable 添加項目。看到本信息,說明該文章來源於網頁教學網www.webjx.com,如果文章不完整請到網頁教學網webjx.com浏覽!
下面的代碼創建一個名為 mycountrIEs 的 Hashtable,並添加了四個元素:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("C","China")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountrIEs.Add("I","Italy")
end if
end sub
</script>
Hashtable 對象可為下面這些控件自動地生成文本和值:
如需把數據綁定到某個 RadioButtonList 控件,首先請在一個 .aspx 頁面中創建 RadioButtonList 控件(沒有任何 ASP:ListItem 元素)
<html> <body> <form runat="server"> <ASP:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </Html>
然後添加構建列表的腳本:本信息代表文章來源網頁教學webjx.com請大家去www.webjx.com浏覽!
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
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 Hashtable
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>
注釋:您無法選擇添加到 Hashtable 的項目的排序方式。如需對項目進行字母排序或數字排序,請使用 SortedList 對象。