ASP.NET - SortedList

يحمل SortedList خصائص كلا ArrayList وHashtable.

الجدول المرتب

يحتوي SortedList على عناصر تمثلها كأزواج مفتاح/قيمة. يمكن لـ SortedList ترتيب العناصر تلقائيًا وفقًا للترتيب الحرفي أو الرقمي.

إضافة العناصر إلى SortedList باستخدام طريقة Add(). يمكن لتصغير الحجم النهائي من SortedList باستخدام طريقة TrimToSize().

هذا الكود ي创建了名为 mycountries SortedList، ويضيف أربعة عناصر:

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

الربط البيانات

The SortedList object can automatically generate text and values for the following controls:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

إذا كنت ترغب في ربط البيانات بـ RadioButtonList، أولاً، قم بإنشاء Radio Button List في ملف aspx (بدون أي عناصر asp:ListItem):

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

ثم إضافة سكريبت بناء القائمة:

<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="Η αγαπημένη σας χώρα είναι: " & 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>

Εμφάνιση Αυτού του Παραδείγματος