Slide 14.4: The Hashtable object
Slide 14.6: XML files
Home

The SortedList Object


The SortedList object combines the features of both the ArrayList object and the Hashtable object. The SortedList object contains items in key/value pairs. A SortedList object automatically sort the items in alphabetic or numeric order. Items are added to the SortedList with the Add method. A SortedList object may automatically generate the text and values to the following controls:
A SortedList can be sized to its final size with the TrimToSize method. The example is similar to the previous example, but uses a SortedList.


demo_sortedlist_drop1.aspx

Web
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="VB" runat="server">
 Sub Page_Load(s As Object, e As EventArgs) Handles Me.Load
   If Not Page.IsPostBack Then
     dim myCountries = New SortedList
     myCountries.Add( "N", "Norway" )
     myCountries.Add( "S", "Sweden" )
     myCountries.Add( "F", "France" )
     myCountries.Add( "I", "Italy"  )
     dd.DataSource     = myCountries
     dd.DataValueField = "Key"
     dd.DataTextField  = "Value"
     dd.DataBind( )
   End If
 End Sub

 Sub DisplayMessage( s as Object,e As EventArgs )
   lbl1.text ="Your favorite country is: " & _
      dd.SelectedItem.Text
 End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
 <body> 
  <form runat="server">
   <asp:DropDownList runat="server" AutoPostBack="True" 
     id="dd" OnSelectedIndexChanged="DisplayMessage" />
   <p><asp:label id="lbl1" runat="server" /></p>
  </form>
 </body>
</html>