ASP.NET - Hashtable Nesnesi
- Önceki Sayfa WebForms ArrayList
- Sonraki Sayfa WebForms SortedList
Hashtable nesnesi anahtar/değer çiftleri ile öğeler içerir.
Hashtable Oluşturma
Hashtable nesnesi anahtar/değer çiftleri ile öğeler içerir. Anahtar, indeks olarak kullanılır ve anahtarı arayarak değere hızlı bir şekilde erişilebilir.
Add() yöntemi ile Hashtable e öğe eklenir.
Aşağıdaki kod, mycountries adında bir Hashtable oluşturur ve dört öğe ekler:
<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>
Veri Bağlama
Hashtable nesnesi aşağıdaki denetimler için otomatik olarak metin ve değer oluşturabilir:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
Bir RadioButtonList denetimi bağlamak için önce bir .aspx sayfasında RadioButtonList denetimi oluşturun (asp:ListItem elementi yok)
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
Listeyi oluşturmak için komut ekle:
<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>
Kullanıcı RadioButtonList denetimi içindeki bir öğeye tıkladığında çalışacak bir alt prosedür ekliyoruz. Bir radyo düğmesi tıklandığında, etikette bir metin görüntülenir:
<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>
Not:Hashtable'a eklenen öğelerin sıralama yöntemini seçemezsiniz. Öğeleri harf sırası veya sayı sırası ile sıralamak için SortedList nesnesini kullanın.
- Önceki Sayfa WebForms ArrayList
- Sonraki Sayfa WebForms SortedList