Using Sorted List Object in ASP.NET
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 can be sized to its final size with the TrimToSize() method.
The following code creates a SortedList named books and three elements are added:
SortedList books =New SortedList
 books .Add(”b1″,”book1″)
 books .Add(”b2″,”book2″)
 books .Add(”b3″,”book3″)
Data Binding:
A SortedList object may automatically generate the text and values to the following controls:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<form runat=”server”>
<asp:RadioButtonList id=”rb” runat=”server”
AutoPostBack=”True” /></form>
Then add the code that builds the list
SortedList books =New SortedList
 books .Add(”b1″,”book1″)
 books .Add(”b2″,”book2″)
  books .Add(”b3″,”book3″)
 rb.DataSource=books
 rb.DataValueField=”Key”
 rb.DataTextField=”Value”
 rb.DataBind()
Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label:
public void displayMessage()
{
lbl1.text=�Your favorite book is: � + rb.SelectedItem.Text
}
Â
