ASP.NET - SortedList
- الصفحة السابقة WebForms Hashtable
- الصفحة التالية WebForms XML file
عنصر SortedList يجمع بين خصائص ArrayList وHashtable.
عنصر 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>
- الصفحة السابقة WebForms Hashtable
- الصفحة التالية WebForms XML file