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")
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 ในหน้า .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>

แสดงตัวอย่างนี้

Property DataSource ของควบคุม RadioButtonList ถูกตั้งเป็น ArrayList ซึ่งกำหนดแหล่งข้อมูลของควบคุม RadioButtonList ตัวมัน วิธี DataBind() ของควบคุม RadioButtonList จะทำให้ควบคุม RadioButtonList และแหล่งข้อมูลเชื่อมโยงกัน

หมายเหตุ:ข้อมูลที่ใช้เป็น Text และ Property Value ของควบคุม หากต้องการเพิ่ม Value ที่ต่างจาก Text แล้ว สามารถใช้ Object Hashtable หรือ Object SortedList ได้