ASP.NET - العنصر ArrayList

العنصر ArrayList هو مجموعة من العناصر تحتوي على قيمة بيانات واحدة.

مثال

ArrayList DropDownList

ArrayList RadioButtonList

إنشاء ArrayList

العنصر ArrayList هو مجموعة من العناصر تحتوي على قيمة بيانات واحدة.

إضافة مشاريع إلى ArrayList باستخدام دالة Add():

يحتوي الكود التالي على إنشاء عنصر ArrayList جديد يُدعى mycountries، وإضافة أربعة مشاريع إليه:

<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")
fin if
fin sub
</script>

بشكل افتراضي، يحتوي العنصر ArrayList على 16 عنصر. يمكنك تعديل حجم ArrayList إلى الحجم النهائي باستخدام دالة TrimToSize():

<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()
fin if
fin sub
</script>

من خلال دالة Sort()، يمكن للعنصر ArrayList أن يتم ترتيب النصوص أو الأرقام حسب الترتيب الأبجدي أو الترتيب الرقمي:

<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()
fin if
fin sub
</script>

لتحقيق ترتيب عكسي، قم بتطبيق دالة Reverse() بعد دالة Sort():

<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()
fin if
fin sub
</script>

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

يمكن للعنصر ArrayList إنشاء النصوص والأرقام تلقائيًا للعناصر التالية:

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

إذا كنت ترغب في ربط البيانات بجهاز التحكم RadioButtonList، يرجى أولاً إنشاء جهاز التحكم RadioButtonList في صفحة .aspx (لاحظ أن لا يوجد عنصر asp:ListItem أيضًا):

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

ثم أضف سكريبت بناء القائمة، وأربط القيم في القائمة إلى هذا RadioButtonList:

<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()
fin if
fin sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

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

تم تعيين خاصية DataSource للـ RadioButtonList إلى هذا ArrayList، مما يعرف مصدر البيانات للـ RadioButtonList. تقوم دالة DataBind() للـ RadioButtonList ب绑定 RadioButtonList إلى مصدر البيانات.

ملاحظة:استخدام قيمة البيانات كـ Text و Value الخاصية للعنصر. إذا كنت ترغب في إضافة Value مختلف عن Text، يمكنك استخدام Object HashTable أو Object SortedList.