ASP.NET - Đối tượng ArrayList

Đối tượng ArrayList là tập hợp các mục chứa giá trị dữ liệu đơn lẻ.

Mẫu

ArrayList DropDownList

ArrayList RadioButtonList

Tạo ArrayList

Đối tượng ArrayList là tập hợp các mục chứa giá trị dữ liệu đơn lẻ.

Thêm mục vào ArrayList bằng phương thức Add().

Mã dưới đây tạo ra một đối tượng ArrayList mới có tên là mycountries và thêm bốn mục:

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

Mặc định, một đối tượng ArrayList chứa 16 mục. Bạn có thể điều chỉnh ArrayList về kích thước cuối cùng bằng phương thức 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>

Bằng cách Sort() phương thức, ArrayList cũng có thể được sắp xếp theo thứ tự chữ cái hoặc số:

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

Để thực hiện việc sắp xếp ngược, hãy áp dụng phương thức Reverse() sau khi Sort() phương thức:

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

Gán dữ liệu cho ArrayList

Đối tượng ArrayList có thể tự động tạo văn bản và giá trị cho các điều khiển sau:

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

Nếu muốn gán dữ liệu cho điều khiển RadioButtonList, trước tiên hãy tạo điều khiển RadioButtonList trong một trang .aspx (lưu ý rằng không có bất kỳ thẻ asp:ListItem nào):

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

Sau đó thêm ván bản viết lại danh sách và gắn kết giá trị trong danh sách với điều khiển 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>

Hiển Thị Mẫu Này

Thuộc tính DataSource của điều khiển RadioButtonList được đặt thành ArrayList này, nó định nghĩa nguồn dữ liệu cho điều khiển RadioButtonList. Phương thức DataBind() của điều khiển RadioButtonList gắn kết điều khiển RadioButtonList với nguồn dữ liệu.

Ghi Chú:Giá trị dữ liệu được sử dụng như thuộc tính Text và Value của điều khiển. Để thêm giá trị khác不同于 Text, bạn có thể sử dụng đối tượng Hashtable hoặc đối tượng SortedList.