ASP.NET - ArrayList nesnesi
- Önceki Sayfa WebForms Veri Bağlama
- Sonraki Sayfa WebForms Hashtable
ArrayList nesnesi, tek bir veri değeri içeren projelerin koleksiyonudur.
ArrayList oluşturma
ArrayList nesnesi, tek bir veri değeri içeren projelerin koleksiyonudur.
ArrayList'e projeler eklemek için Add() yöntemini kullanın.
Aşağıdaki kod, adı mycountries olan yeni bir ArrayList nesnesi oluşturur ve dört projeye ekler:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") end if end sub </script>
Öntanımlı olarak, bir ArrayList nesnesi 16 adet girdi içerir. TrimToSize() yöntemi ile ArrayList'i nihai boyutuna ayarlayabilirsiniz:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() end if end sub </script>
Sort() yöntemi sayesinde, ArrayList harf sırası veya sayı sırasına göre sıralanabilir:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() end if end sub </script>
Tersten sıralama gerçekleştirmek için, Sort() yöntemini ardından Reverse() yöntemini uygulayın:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() end if end sub </script>
ArrayList'e veri bağlamak
ArrayList nesnesi aşağıdaki denetimlere otomatik olarak metin ve değer oluşturabilir:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
RadioButtonList denetimiye veri bağlamak için, öncelikle bir .aspx sayfasında RadioButtonList denetimi oluşturun (lütfen, hiçbir asp:ListItem elementi olmamalıdır):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html>
Sonra listeyi oluşturacak betiği ekleyin ve listedeki değerleri bu RadioButtonList kontrolüne bağlayın:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() rb.DataSource=mycountries rb.DataBind() end if end sub </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html>
RadioButtonList kontrolünün DataSource özelliği bu ArrayList olarak ayarlanmıştır, bu RadioButtonList kontrolünün veri kaynağını tanımlar. RadioButtonList kontrolünün DataBind() metodu, RadioButtonList kontrolünü veri kaynağı ile birleştirir.
Açıklama:Veri değeri, kontrolün Text ve Value özellikleri olarak kullanılır. Text'ten farklı bir Value eklemek için, Hashtable nesnesi veya SortedList nesnesi kullanılabilir.
- Önceki Sayfa WebForms Veri Bağlama
- Sonraki Sayfa WebForms Hashtable