ASP.NET - SortedList

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

عنصر SortedList

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

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

تأسس الكود التالي SortedList يُدعى mycountries ويضيف أربعة عناصر:

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

ربط البيانات

يمكن لـ SortedList أن يولد تلقائيًا النصوص والقيم للعناصر التالية:

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

إذا كنت ترغب في ربط البيانات بـ RadioButtonList، أولاً، قم بإنشاء RadioButtonList في ملف 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="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>

اعرض هذا المثال