最新のWeb開発のチュートリアル
 

ASP.NET Webフォーム - Hashtableオブジェクト


Hashtableオブジェクトは、キー/値ペア内の項目が含まれています。


例

HashtableのRadioButtonListの1

ハッシュテーブルのDropDownList


ハッシュテーブルを作成します。

Hashtableオブジェクトは、キー/値ペア内の項目が含まれています。 キーがインデックスとして使用され、非常に迅速な検索が彼らのキーを検索して値のために行うことができます。

アイテムは、ハッシュテーブルに追加されたAdd()メソッド。

次のコードは、ハッシュテーブルの名前mycountriesを作成し、4つの要素が追加されます。

<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オブジェクトは、自動的に次のコントロールにテキストと値を生成することがあります。

  • ASP:RadioButtonListの
  • ASP:CheckBoxListの
  • ASP:DropDownListコントロール
  • ASP:リストボックス

RadioButtonListコントロールにデータをバインドするには、最初のRadioButtonListコントロールの作成(without any asp:ListItem elements) .aspxページ内の:

<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("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>

その後、我々は、ユーザが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

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>
»例を表示

Note:あなたはハッシュテーブルに追加された項目の並び順を選択することはできません。 アルファベット順または数値的に項目を並べ替えるには、SortedListのオブジェクトを使用します。