SortedLists

A SortedList is a storage array of items combining features of both the HashTable and sorted ArrayList. It contains items representing key/value pairs. The keys are the indexes to their related values. Like a HashTable used to bind values to a control, the keys and/or values in a SortedList become the Text and/or Value properties for the control. A SortedList automatically maintains its order of items by the alphabetic or numeric order of its keys.

Figure C-12. Using a SortedList for data binding.

Creating a SortedList

A SortedList is created by assigning a New SortedList() object to a reference variable, optionally specifying the number of entries in the list within the parentheses. Then, data values are added to the list with the list's Add() method, supplying a key and associated value. Once the SortedList is filled with data it can be bound to a list control. A SortedList is bound to the DataSource property of the control. Assignment of the SortedList's keys and/or values are made to the control's Text property (DataTextField) and Value property (DataValueField). The control's DataBind() method is called to bind the values. General formats for the statements involved in binding a SortedList to a list control are shown below.

Dim SortedList = New SortedList()

SortedList.Add(key,value)
...

Control.DataSource = SortedList
Control.DataTextField = "Key|Value"
Control.DataValueField = "Key|Value"
Control.DataBind()

Figure C-13. General formats for creating and binding a SortedList.

The following code creates a SortedList named URLs to which six entries are added. The keys are string names of Web search sites; the values are the URL addresses of the associated sites.

<SCRIPT Runat="Server">

Sub Page_Load

  If Not Page.IsPostBack Then

    Dim URLs = New SortedList()
    URLs.Add("Google", "http://www.google.com")
    URLs.Add("MSN", "http://search.msn.com")
    URLs.Add("Yahoo", "http://www.yahoo.com")
    URLs.Add("Lycos", "http://www.lycos.com")
    URLs.Add("AltaVista", "http://www.altavista.com")
    URLs.Add("Excite", "http://www.excite.com")

  End If

End Sub

</SCRIPT>

Listing C-15. Script to load a SortedList.

By default, a new SortedList contains 16 elements; however, it expands automatically as items are added to the list. This value becomes the muliplier for expanding the list when adding an item beyond the last available slot. A SortedList can be compacted to its final required size. The TrimToSize() method is applied to set the capacity of the list to its current number of items, which is give by its Count property. A SortedList does not require sorting since items are automatically maintained in alphabetic or numeric order by key values.

Binding a SortedList to Controls

A SortedList can be used to provide the text labels and values that are automatically assigned to list controls. In the following example, these controls are populated from a SortedList, and buttons are provided for scripting page transfers to selected URLs.

asp:RadioButtonList asp:CheckBoxList asp:DropDownList asp:ListBox


Figure C-14. Binding a SortedList to server controls.

Below is the Page_Load script that creates a SortedList and binds its Key and Value properties to the Text and Value properties to the target controls. The controls are assigned preselected values so that the subprograms to open search sites do not have to check for nonselections. This code is virtually identical to that used previously for a HashTable.

<SCRIPT Runat="Server">

Sub Page_Load

  If Not Page.IsPostBack Then

    Dim URLs = New SortedList()
    URLs.Add("Google", "http://www.google.com")
    URLs.Add("MSN", "http://search.msn.com")
    URLs.Add("Yahoo", "http://www.yahoo.com")
    URLs.Add("Lycos", "http://www.lycos.com")
    URLs.Add("AltaVista", "http://www.altavista.com")
    URLs.Add("Excite", "http://www.excite.com")

    '-- Bind SortedList to controls

    RadioButtons.DataSource = URLs
    RadioButtons.DataTextField = "Key"
    RadioButtons.DataValueField = "Value"
    RadioButtons.DataBind()
    RadioButtons.Items(0).Selected = True

    CheckBoxes.DataSource = URLs
    CheckBoxes.DataTextField = "Key"
    CheckBoxes.DataValueField = "Value"
    CheckBoxes.DataBind()
    CheckBoxes.Items(0).Selected = True

    DropDownList.DataSource = URLs
    DropDownList.DataTextField = "Key"
    DropDownList.DataValueField = "Value"
    DropDownList.DataBind()
    DropDownList.Items(0).Selected = True

    ListBox.DataSource = URLs
    ListBox.DataTextField = "Key"
    ListBox.DataValueField = "Value"
    ListBox.DataBind()
    ListBox.Items(0).Selected = True

  End If

End Sub

Sub GetRadioButtonURL(Src as Object, Args As EventArgs)
  Response.Redirect(RadioButtons.SelectedValue)
End Sub

Sub GetCheckBoxURL(Src as Object, Args As EventArgs)
  Response.Redirect(CheckBoxes.SelectedValue)
End Sub

Sub GetDropDownListURL(Src as Object, Args As EventArgs)
  Response.Redirect(DropDownList.SelectedValue)
End Sub

Sub GetListBoxURL(Src as Object, Args As EventArgs)
  Response.Redirect(ListBox.SelectedValue)
End Sub

</SCRIPT>

<form Runat="Server">

<asp:RadioButtonList id="RadioButtons" Runat="Server"/>
<asp:Button Text="Go to Site" OnClick="GetRadioButtonURL" Runat="Server"/>

<asp:CheckBoxList id="CheckBoxes" Runat="Server"/>
<asp:Button Text="Go to Site" OnClick="GetCheckBoxURL" Runat="Server"/>

<asp:DropDownList id="DropDownList" Runat="Server"/>
<asp:Button Text="Go to Site" OnClick="GetDropDownListURL" Runat="Server"/>

<asp:ListBox id="ListBox" Runat="Server"/>
<asp:Button Text="Go to Site" OnClick="GetListBoxURL" Runat="Server"/>

</form>
Listing C-16. Code to bind a SortedList to server controls.

The script which builts the SortedList includes code to bind the keys and values in the list to the Text and Value properties of the controls. The control's DataSource property identifies the SortedList as the source for data binding. DataTextField specifies the first of the pair of SortedList items ("Key") as the text label for the control; DataValueField specifies the second of the pair ("Value") as the value for the control. The order of the list is the sorted order of keys of the SortedList.

It is not necessary to save this SortedList in View State. The list is created only the first time the page loads. It is used to populate the four list controls but is not recreated and will not be needed on subsequent post-backs.Its keys and values populate the list controls, which do take part in View State and will be available on subsequent page loads.

As in the case of HashTable binding, the Response.Redirect("url") method redirects to the site specified in the url parameter which, in turn, is supplied by the associated SelectedValue of the control.

Accessing SortedLists

Like a HashTable, a SortedList can be used as a look-up table for extracting values for supplied keys. Previous lookup code for a HashTable can be used for a SortedList with only the need to change references from HashTable to SortedList.