Sortedlist objects have the features of arraylist and hashtable objects.
The sortedlist object can automatically generate text and values for the following controls:
ASP: radiobuttonlist
ASP: checkboxlist
ASP: dropdownlist
ASP: ListBox
The sortedlist object contains items represented by key/value pairs. Sortedlist objects can be automatically sorted by character or number.
Use the add () method to add a project to sortedlist. Sortedlist can be adjusted to the final size by using the trimtosize () method.
The followingCodeA sortedlist named mycountries is created and four elements are added:
To bind data to the radiobuttonlist control, first create a radiobuttonlist control in the aspx file (without any ASP: listitem element ):
<HTML>
<Body>
<Form runat = "server">
<Asp: radiobuttonlist id = "rb" runat = "server" autopostback = "true"/>
</Form>
</Body>
</Html>
Then add the build list script:
<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>
Then we add a subroutine that will be executed when the user clicks the project in the radiobuttonlist control. When the single-choice button is clicked, the text will appear in the label:
<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>