ASP.NET - Objeto SortedList
- Página Anterior SortedList de WebForms
- Página Siguiente Repeater de WebForms
El objeto SortedList tiene las características de ArrayList y Hashtable.
Objeto SortedList
El objeto SortedList contiene elementos representados por pares de clave/valor. El objeto SortedList puede ordenar automáticamente los elementos en orden alfabético o numérico.
Se añaden elementos al SortedList mediante el método Add(). SortedList puede ajustarse al tamaño final con el método TrimToSize().
El siguiente código crea un SortedList llamado mycountries y agrega cuatro elementos:
<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") fin if fin sub </script>
Enlazado de datos
El objeto SortedList puede generar automáticamente texto y valores para los siguientes controles:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
Si deseas enlazar los datos al control RadioButtonList, primero crea un RadioButtonList en el archivo aspx (sin elementos asp:ListItem):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
Luego, agrega el script para construir la lista:
<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() fin if fin sub </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
Luego agregamos un subproceso, que se ejecutará cuando el usuario haga clic en un elemento del control RadioButtonList. Cuando se haga clic en el botón de opción, el texto aparecerá en el 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() fin if fin sub sub displayMessage(s como Object, e como EventArgs) lbl1.text="Su país favorito es: " & rb.SelectedItem.Text fin 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>
- Página Anterior SortedList de WebForms
- Página Siguiente Repeater de WebForms