ASP.NET Hashtable

Hashtable วัตถุมีรายการที่มีคีย์ / คู่ค่าที่แสดง


ตัวอย่าง

ลอง - ตัวอย่าง

Hashtable RadioButtonList 1

Hashtable RadioButtonList 2

Hashtable DropDownList


สร้าง Hashtable

Hashtable วัตถุมีรายการที่มีคีย์ / คู่ค่าที่แสดง ที่สำคัญคือการใช้เป็นดัชนีโดยค่าการค้นหาคีย์ที่สามารถประสบความสำเร็จในการค้นหาอย่างรวดเร็ว

เพิ่มรายการไปยัง Hashtable ผ่าน Add () วิธีการ

รหัสต่อไปนี้สร้างวัตถุ Hashtable เรียก mycountries และเพิ่มธาตุทั้งสี่:

<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 ในเพจที่มีขอบ (ไม่ ASP ใดองค์ประกอบ ListItem):

<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
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>

จากนั้นเราก็เพิ่มย่อยเมื่อผู้ใช้คลิกที่รายการในการควบคุม RadioButtonList เมื่อ subroutine ที่จะดำเนินการ เมื่อปุ่มมีการคลิกที่ชื่อจะปรากฏในบรรทัดของข้อความ:

ตัวอย่าง

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

การสาธิต >>

หมายเหตุ: คุณไม่สามารถเลือกที่จะเพิ่มรายการ Hashtable จะเรียงสำหรับโครงการหรือตัวอักษรลำดับตัวเลขใช้วัตถุ SortedList