ASP.NET Web Forms - SortedList object
An ASP.NETSortedList object represents a collection of key/value pairs that are sorted by key and accessible by key and index.
SortedList objects combine the characteristics of ArrayList objects and Hashtable objects.
Example
SortedList RadiobuttonList 1
SortedList RadiobuttonList 2
SortedList DropDownList
SortedList object
A SortedList object contains items represented by key/value pairs. A SortedList object automatically sorts items in alphabetical or numerical order.
Add items to the SortedList through the Add() method. Adjust the SortedList to its final size through the TrimToSize() method.
The following code creates a SortedList object named mycountries and adds four elements:
<script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New SortedListmycountries.Add("N","Norway")mycountries.Add("S","Sweden")mycountries.Add("F", "France")mycountries.Add("I","Italy")end ifend sub</script>data binding
SortedList objects automatically generate text and values for the following controls:
asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
In order to bind data to the RadioButtonList control, first create a RadioButtonList control in the .aspx page (without any asp:ListItem elements):
<html><body><form runat="server"><asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /></form></body></html>Then add the script that creates the list and bind the values in the list to the RadioButtonList control:
<script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New SortedListmycountries.Add("N","Norway")mycountries.Add("S","Sweden")mycountries.Add("F","France")mycountries.Add("I","Italy")rb. DataSource=mycountriesrb.DataValueField="Key"rb.DataTextField="Value"rb.DataBind()end ifend sub</script><html><body><form runat="server"><asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /></form></body></ html>Then we add a subroutine that will be executed when the user clicks an item in the RadioButtonList control. When a radio button is clicked, a line of text will appear in the label:
Example
<script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New SortedListmycountries.Add("N","Norway")mycountries.Add("S","Sweden")mycountries.Add("F","France")mycountries.Add("I","Italy")rb. DataSource=mycountriesrb.DataValueField="Key"rb.DataTextField="Value"rb.DataBind()end ifend subsub displayMessage(s as Object,e As EventArgs)lbl1.text="Your favorite country is: " & rb.SelectedItem.Textend 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>The above is about the use of ASP.NETSortedList object.