欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET - Hashtable 對象

Hashtable 對象包含用鍵/值對表示的項目。

創(chuàng)建 Hashtable

Hashtable 對象包含用鍵/值對表示的項目。鍵被用作索引,通過搜索其鍵,可以實現(xiàn)對值的快速搜索。

通過 Add() 方法向 Hashtable 添加項目。

下面的代碼創(chuàng)建一個名為 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>

數(shù)據綁定

Hashtable 對象可為下面這些控件自動地生成文本和值:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

如需把數(shù)據綁定到某個 RadioButtonList 控件,首先請在一個 .aspx 頁面中創(chuàng)建 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 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 控件中的某個項目時被執(zhí)行。當某個單選按鈕被點擊,label 中會出現(xiàn)一條文本:

<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 的項目的排序方式。如需對項目進行字母排序或數(shù)字排序,請使用 SortedList 對象。