ASP.NET - SortedList 객체

SortedList 객체는 ArrayList와 Hashtable 객체의 특성을 모두 가지고 있습니다.

SortedList 객체

SortedList 객체는 키/값 쌍으로 표현된 항목을 포함합니다. SortedList 객체는 문자 순서나 숫자 순서로 자동으로 항목을 정렬할 수 있습니다.

Add() 메서드를 사용하여 SortedList에 항목을 추가합니다. SortedList는 TrimToSize() 메서드를 사용하여 최종 크기를 조정할 수 있습니다.

아래 코드는 mycountries라는 SortedList를 생성하고 네 가지 요소를 추가합니다:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
end if
end sub
</script>

데이터 바인딩

SortedList 객체는 자동으로 아래의 컨트롤에 텍스트와 값을 생성할 수 있습니다:

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

RadioButtonList 컨트롤에 데이터를 바인딩하려면, 먼저 aspx 파일에서 RadioButtonList 컨트롤을 생성하세요 (asp:ListItem 요소가 없습니다):

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

그런 다음, 목록을 구성하는 스크립트를 추가하세요:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>

그런 다음 우리는 사용자가 RadioButtonList 컨트롤의 항목을 클릭할 때 실행되는 서브루틴을 추가합니다. 단일 선택 버튼이 클릭될 때, 텍스트가 레이블에 나타납니다:}}

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add("C","China")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>

이 예제를 표시