ASP.NET - ArrayList ບັນດາອັນ

ArrayList ບັນດາອັນແມ່ນກຸ່ມບັນດາອັນທີ່ມີຄວາມຄິດຫຼັງກັນດຽວ. ສ້າງ ArrayList:

ບັນດາຄວາມນັບສະຖານະ

ArrayList DropDownList

ArrayList RadioButtonList

ສ້າງ ArrayList

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

ມັດຖະໜົນຂໍ້ມູນໃຫ້ ArrayList

ArrayList ບັນດາອັນສາມາດມີການສ້າງຂໍ້ຄວາມ ແລະ ຄູ່ມູນອັນໄດ້ອອກມາໃຫ້ແກ່ກົວສັບດັ່ງຕໍ່ມາ:

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

ສຳລັບການມັດຖະໜົນຂໍ້ມູນໃຫ້ແກ່ກົວສັບ RadioButtonList (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()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

ສະແດງຄວາມຄົນນີ້

ຂັດຂອງ DataSource ຂອງ RadioButtonList ຖືກກຳນົດໃຫ້ຢູ່ ArrayList, ທີ່ກໍານົດຂໍ້ມູນຂອງ RadioButtonList. ຂັດຂອງ DataBind() ຂອງ RadioButtonList ກຳນົດການຕິດຕໍ່ຂອງ RadioButtonList ກັບຂໍ້ມູນຂອງຂໍ້ມູນ.

ຄວາມເຫັນ:ຂັດຂອງຄວາມມູນທີ່ຖືກໃຊ້ຄືຄວາມຂອງວຽງແລະຄວາມຂອງວຽງ. ຖ້າຕ້ອງໃຫ້ມີຄວາມມູນທີ່ບໍ່ຕາມ Text, ທ່ານສາມາດໃຊ້ບັນດາບັນດາວຽງ HashTable ແລະ SortedList.