ASP.NET - SortedList nesnesi

SortedList nesnesi, ArrayList ve Hashtable nesnelerinin özelliklerini taşır.

SortedList nesnesi

SortedList nesnesi, anahtar/değer çiftleri ile temsil edilen öğeleri içerir. SortedList nesnesi, öğeleri karakter sırasına veya sayısal sıraya otomatik olarak sıralayabilir.

Add() metodu ile SortedList e öğe eklenir. SortedList, TrimToSize() metodu ile son boyuta ayarlanabilir.

Aşağıdaki kod, adı mycountries olan bir SortedList oluşturur ve dört öğe ekler:

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

Veri Bağlama

SortedList 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 istiyorsanız, öncelikle aspx dosyasında bir RadioButtonList denetimi oluşturun (hiçbir asp:ListItem elementi olmamalı):

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

Sonra oluşturma listesine ekleyin:

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

Sonra kullanıcı RadioButtonList denetimindeki bir öğeye tıkladığında çalışacak bir alt program ekleriz. Single seçim düğmesi tıklandığında, metin label'da görünür olacaktır:}}

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

Bu Örneği Göster