ASP.NET - ArrayList 객체
- 이전 페이지 WebForms 데이터 바인딩
- 다음 페이지 WebForms Hashtable
ArrayList 객체는 단일 데이터 값이 포함된 항목의 집합입니다.
ArrayList 생성
ArrayList 객체는 단일 데이터 값이 포함된 항목의 집합입니다.
Add() 메서드를 사용하여 ArrayList에 항목을 추가합니다.
다음 코드는 새로운 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개의 항목을 포함하고 있습니다. TrimToSize() 메서드를 사용하여 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() 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>
역순 정렬을 구현하려면, Sort() 메서드 후 Reverse() 메서드를 적용하십시오:
<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 컨트롤에 바인딩하려면, 먼저 .aspx 페이지에서 RadioButtonList 컨트롤을 생성하십시오. (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>
RadioButtonList控件의 DataSource 속성이 ArrayList로 설정되어 해당 RadioButtonList控件의 데이터 소스를 정의합니다. RadioButtonList控件의 DataBind() 메서드는 RadioButtonList控件와 데이터 소스를 연결합니다.
주의사항:데이터 값은控件의 Text 및 Value 속성으로 사용됩니다. Text와 다른 Value를 추가하려면 Hashtable 객체 또는 SortedList 객체를 사용할 수 있습니다.
- 이전 페이지 WebForms 데이터 바인딩
- 다음 페이지 WebForms Hashtable