Web 表單哈希表
ASP.NET Web Forms - Hashtable 物件
#Hashtable 物件包含以鍵/值物件表示的項目。
試試看- 實例
Hashtable RadiobuttonList 1
Hashtable RadiobuttonList 2
#Hashtable DropDownList
建立Hashtable
Hashtable 物件包含以鍵/值對錶示的項目。鍵被用作索引,透過搜尋鍵,可以實現對值的快速搜尋。
透過 Add() 方法將項目新增至 Hashtable。
下面的程式碼建立了一個名為mycountries 的Hashtable 對象,並加入了四個元素:
<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>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
資料綁定Hashtable 物件可為下列的控制項自動產生文字和值:
- asp:RadioButtonListasp:CheckBoxListasp:DropDownList
- asp:Listbox
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#</body>
然後新增建立清單的腳本,並且綁定清單中的值到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>
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>
</html>
#然後我們加入子例程,當使用者點選RadioButtonList 控制項中的某個項目時,該子程式會被執行。當某個單選按鈕被點擊時,label 中會出現一行文字:
實例#
<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 的項目的排序方式。如需對項目進行字母排序或數字排序,請使用 SortedList 物件。