ASP.NET Web Forms - Objeto SortedList


El objeto SortedList combina las características del objeto ArrayList y del objeto Hashtable.


Examples

Pruébalo - Ejemplo

SortedList RadiobuttonList 1

SortedList RadiobuttonList 2

SortedList DropDownList


Objeto SortedList

El objeto SortedList contiene elementos representados por pares clave/valor. El objeto SortedList ordena automáticamente los elementos en orden alfabético o numérico.

Agregue elementos al SortedList mediante el método Add(). Ajuste el SortedList a su tamaño final mediante el método TrimToSize().

El siguiente código crea un objeto SortedList llamado mycountries y agrega cuatro elementos:

<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>


Enlace de datos

El objeto SortedList puede generar automáticamente texto y valores para los siguientes controles:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

Para enlazar datos al control RadioButtonList, primero cree un control RadioButtonList (sin ningún elemento asp:ListItem) en la página .aspx:

<html>
<body>

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

</body>
</html>

Luego agregue el script que crea la lista y enlace los valores de la lista al control 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
</script>

<html>
<body>

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

</body>
</html>

Luego agregamos una subrutina que se ejecutará cuando el usuario haga clic en un elemento del control RadioButtonList. Cuando se hace clic en un botón de opción, aparece una línea de texto en el control Label:

Ejemplo

<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>

Demostración del ejemplo »

Otras extensiones