?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
Hashtable 對(duì)象包含用鍵/值對(duì)表示的項(xiàng)目。
Hashtable RadiobuttonList 1
<script??runat="server"> sub?Page_Load if?Not?Page.IsPostBack?then ???dim?mycountries=New?Hashtable ???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>
Hashtable RadiobuttonList 2
<script??runat="server"> sub?Page_Load if?Not?Page.IsPostBack?then ???dim?navigate=New?Hashtable ???navigate.Add("RadioButtonList","control_radiobuttonlist.asp") ???navigate.Add("CheckBoxList","control_checkboxlist.asp") ???navigate.Add("DropDownList","control_dropdownlist.asp") ???navigate.Add("ListBox","control_listbox.asp") ???rb.DataSource=navigate ???rb.DataValueField="Value" ???rb.DataTextField="Key" ???rb.DataBind() end?if end?sub sub?navigate(s?as?Object,?e?As?EventArgs) response.redirect(rb.SelectedItem.Value) end?sub </script> <!DOCTYPE?html> <html> <body> <form?runat="server"> <asp:RadioButtonList?id="rb"?runat="server" AutoPostBack="True"?onSelectedIndexChanged="navigate"?/> </form> </body> </html>
Hashtable DropDownList
<script??runat="server"> sub?Page_Load if?Not?Page.IsPostBack?then ???dim?mycountries=New?Hashtable ???mycountries.Add("N","Norway") ???mycountries.Add("S","Sweden") ???mycountries.Add("F","France") ???mycountries.Add("I","Italy") ???dd.DataSource=mycountries ???dd.DataValueField="Key" ???dd.DataTextField="Value" ???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>
Hashtable 對(duì)象包含用鍵/值對(duì)表示的項(xiàng)目。鍵被用作索引,通過(guò)搜索鍵,可以實(shí)現(xiàn)對(duì)值的快速搜索。
通過(guò) Add() 方法向 Hashtable 添加項(xiàng)目。
下面的代碼創(chuàng)建了一個(gè)名為 mycountries 的 Hashtable 對(duì)象,并添加了四個(gè)元素:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
Hashtable 對(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" AutoPostBack="True" />
</form>
</body>
</html>
然后添加創(chuàng)建列表的腳本,并且綁定列表中的值到 RadioButtonList 控件:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
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>
然后我們添加一個(gè)子例程,當(dāng)用戶點(diǎn)擊 RadioButtonList 控件中的某個(gè)項(xiàng)目時(shí),該子例程會(huì)被執(zhí)行。當(dāng)某個(gè)單選按鈕被點(diǎn)擊時(shí),label 中會(huì)出現(xiàn)一行文本:
?<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
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>
注釋:您無(wú)法選擇添加到 Hashtable 的項(xiàng)目的排序方式。如需對(duì)項(xiàng)目進(jìn)行字母排序或者數(shù)字排序,請(qǐng)使用 SortedList 對(duì)象。