ASP.NET - Hashtable ໂອວຄະນະ

Hashtable ໂອວຄະນະວັດທະນະທຳບັນຈຸ/ຄູ່ມູນ. ວັດທະນະທຳຖືກໃຊ້ງານທີ່ສະເພາະເພື່ອການຄົ້ນຫາຄູ່ມູນຢ່າງວ່ອງວາຍ.

ສ້າງ Hashtable

Hashtable ໂອວຄະນະວັດທະນະທຳບັນຈຸ/ຄູ່ມູນ. ວັດທະນະທຳຖືກໃຊ້ງານທີ່ສະເພາະເພື່ອການຄົ້ນຫາຄູ່ມູນຢ່າງວ່ອງວາຍ.

ເພີ່ມຕົວປະກອບວັດທະນະທຳ Add()

ລະບົບທີ່ຖືກສ້າງຊື່ວ່າ 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 ໂອວຄະນະສາມາດສ້າງຕົວອອກຕົວກັບວັດທະນະທຳແລະຄູ່ມູນອັນລາຍລະອຽດນັ້ນຄື:

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

ສຳລັບທີ່ຈະມັດຖະບັນຍັດຂໍ້ມູນຫາ RadioButtonList 控件 ກໍ່ຕ້ອງສ້າງ RadioButtonList 控件ໃນເວລາ .aspx (ບໍ່ມີ 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 控件. ເມື່ອບ່ອນຄົງໜຶ່ງຖືກຄົງ 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