ASP.NET - SortedList na bagay
- หน้าก่อนหน้า Hashtable ng WebForms
- หน้าต่อไป File ng XML sa WebForms
Ang SortedList na bagay ay may katangian ng ArrayList at HashTable na bagay.
Ang SortedList na bagay
Ang SortedList na bagay ay naglalaman ng mga item na inilalarawan ng pares ng key/value. Ang SortedList na bagay ay maaaring ayusin ang mga item sa awtomatiko, alinsunod sa pagkakasunod-sunod ng mga character o alinsunod sa pagkakasunod-sunod ng numero.
Magdagdag ng item sa SortedList sa pamamagitan ng paraan na Add(). Ang SortedList ay maaaring ayusin sa huling laki sa pamamagitan ng TrimToSize() na paraan.
Ang sumusunod na code ay naglikha ng isang SortedList na may pangalan na mycountries at nagdagdag ng apat na elemento:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") end if end sub </script>
Data Binding
Ang SortedList na bagay ay maaaring mag-generate ng teksto at halaga para sa mga sumusunod na kontrol ng awtomatiko:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
Kung gusto mong i-bind ang data sa kontrol na RadioButtonList, unang-una, lumikha ng kontrol na RadioButtonList sa file na aspx (walang asp:ListItem na elemento):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
Pagkatapos, magdagdag ng script ng pagbuo ng list:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList 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 SortedList 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 ng WebForms
- หน้าต่อไป File ng XML sa WebForms