ASP.NET - Objeto ArrayList
- Página anterior Enlazado de datos de WebForms
- Página siguiente Hashtable de WebForms
El objeto ArrayList es una colección de proyectos que contienen un solo valor de datos.
Crear ArrayList
El objeto ArrayList es una colección de proyectos que contienen un solo valor de datos.
Agregar proyectos al ArrayList mediante el método Add():
El siguiente código crea un nuevo objeto ArrayList llamado mycountries y agrega cuatro proyectos:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") fin if fin sub </script>
Por defecto, un objeto ArrayList contiene 16 elementos. Puede ajustar ArrayList al tamaño final mediante el método TrimToSize():
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() fin if fin sub </script>
A través del método Sort(), ArrayList también puede ordenarse en orden alfabético o numérico:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() fin if fin sub </script>
Para lograr un orden invertido, aplique el método Reverse() después del método Sort():
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() fin if fin sub </script>
Vincular datos a ArrayList
El objeto ArrayList puede generar automáticamente texto y valores para los siguientes controles:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
Para vincular datos a un control RadioButtonList, primero cree un control RadioButtonList en una página .aspx (tenga en cuenta que no hay ningún elemento asp:ListItem):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html>
Luego, agregar el script para construir la lista y vincular los valores de la lista al control RadioButtonList: }}
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("China") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() rb.DataSource=mycountries rb.DataBind() fin if fin sub </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html>
El atributo DataSource del control RadioButtonList se establece en este ArrayList, que define la fuente de datos del control RadioButtonList. El método DataBind() del control RadioButtonList une el control RadioButtonList con la fuente de datos.
Notas:El valor de datos se utiliza como las propiedades Text y Value del control. Si se desea agregar un valor diferente al Text, se puede usar el objeto Hashtable o el objeto SortedList.
- Página anterior Enlazado de datos de WebForms
- Página siguiente Hashtable de WebForms