ASP.NET - The SortedList Object

1.Example 1 - SortedList RadioButtonList

 

<script  runat="server">
sub Page_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")
   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>

Output Result:

 

when you click one of  the radios ,it will shows:(example:you have clicked the radio 'france')

Your favorite country is: France

 

2.Example 2 - SortedList RadiobuttonList

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim navigate
=New SortedList
   navigate.Add(
"RadioButtonList","control_radiobuttonlist.asp")
   navigate.Add(
"CheckBoxList","control_checkboxlist.asp")
   navigate.Add(
"DropDownList","control_dropdownlist.asp")
   navigate.Add(
"ListBox","control_listbox.asp")
   rb.DataSource
=navigate
   rb.DataValueField
="Value"
   rb.DataTextField
="Key"
   rb.DataBind()
end 
if
end sub

sub navigate(s 
as Object, e As EventArgs)
response.redirect(rb.SelectedItem.Value)
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack
="True" onSelectedIndexChanged="navigate" />
</form>

</body>
</html>

Output Result:

 

when you click one of  the radios ,it will shows:(example:you have clicked the radio 'CheckBoxList')

the hamepage will redirect to the url 'control_checkboxlist.asp'

3.Example 3 - SortedList DropDownList

<script  runat="server">
sub Page_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>
<body>

<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack
="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Output Result:


when you click one of  the selects ,it will shows:(example:you have clicked the option 'france')

Your favorite country is: France

 

The SortedList 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 can be sized to its final size with the TrimToSize() method.

The following code creates a SortedList named mycountries and four elements are added:

<script runat="server">
            sub Page_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")
            end if
            end sub
            </script>


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:

<html>
            <body>
<form runat="server">
            <asp:RadioButtonList id="rb" runat="server"
            AutoPostBack="True" />
</form>
</body>
            </html>

Then add the script that builds the list:

<script runat="server">
            sub Page_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")
            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 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:

<script runat="server">
            sub Page_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")
            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>

posted on 2007-03-06 15:33  改变热爱  阅读(176)  评论(0)    收藏  举报

导航