DropDownList (Clase)
Actualización: noviembre 2007
Representa un control que permite al usuario seleccionar un único elemento de una lista desplegable.
Ensamblado: System.Web (en System.Web.dll)
[ValidationPropertyAttribute("SelectedItem")] [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class DropDownList : ListControl, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class DropDownList extends ListControl implements IPostBackDataHandler
public class DropDownList extends ListControl implements IPostBackDataHandler
<asp:DropDownList />
Utilice el control DropDownList para crear un control de lista desplegable de selección única. Para controlar la apariencia del control DropDownList, establezca las propiedades BorderColor, BorderStyle y BorderWidth.
Para especificar los elementos que deben aparecer en el control DropDownList, coloque un objeto ListItem, por cada entrada, entre las etiquetas de apertura y de cierre del control DropDownList.
El control DropDownList admite asimismo el enlace de datos. Para enlazar el control a un origen de datos, cree un origen de datos, como un objeto System.Collections.ArrayList, que contenga los elementos que se van a mostrar en el control. A continuación, utilice el método Control.DataBind para enlazar el origen de datos al control DropDownList.
Utilice la propiedad SelectedIndex para determinar mediante programación el índice del elemento seleccionado por el usuario en el control DropDownList.
Accesibilidad
Es posible que el marcado que se representa de manera predeterminada para este control no respete los estándares de accesibilidad enunciados en las directrices prioritarias de WCAG (Instrucciones de accesibilidad a contenido Web 1.0). Para obtener detalles sobre accesibilidad compatible con este control, vea Controles y accesibilidad en ASP.NET.
En el ejemplo de código siguiente se muestra cómo crear un control DropDownList que contenga cuatro elementos.
<%@ Page Language="C#" AutoEventWireup="True" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <script runat="server" > void Selection_Change(Object sender, EventArgs e) { // Set the background color for days in the Calendar control // based on the value selected by the user from the // DropDownList control. Calendar1.DayStyle.BackColor = System.Drawing.Color.FromName(ColorList.SelectedItem.Value); } </script> <head runat="server"> <title> DropDownList Example </title> </head> <body> <form id="form1" runat="server"> <h3> DropDownList Example </h3> Select a background color for days in the calendar. <br /><br /> <asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server"/> <br /><br /> <table cellpadding="5"> <tr> <td> Background color: </td> </tr> <tr> <td> <asp:DropDownList id="ColorList" AutoPostBack="True" OnSelectedIndexChanged="Selection_Change" runat="server"> <asp:ListItem Selected="True" Value="White"> White </asp:ListItem> <asp:ListItem Value="Silver"> Silver </asp:ListItem> <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem> <asp:ListItem Value="Khaki"> Khaki </asp:ListItem> <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem> </asp:DropDownList> </td> </tr> </table> </form> </body> </html>
En el ejemplo de código siguiente se muestra cómo crear un control DropDownList a través del enlace de datos.
<%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <script runat="server" > void Selection_Change(Object sender, EventArgs e) { // Set the background color for days in the Calendar control // based on the value selected by the user from the // DropDownList control. Calendar1.DayStyle.BackColor = System.Drawing.Color.FromName(ColorList.SelectedItem.Value); } void Page_Load(Object sender, EventArgs e) { // Load data for the DropDownList control only once, when the // page is first loaded. if(!IsPostBack) { // Specify the data source and field names for the Text // and Value properties of the items (ListItem objects) // in the DropDownList control. ColorList.DataSource = CreateDataSource(); ColorList.DataTextField = "ColorTextField"; ColorList.DataValueField = "ColorValueField"; // Bind the data to the control. ColorList.DataBind(); // Set the default selected item, if desired. ColorList.SelectedIndex = 0; } } ICollection CreateDataSource() { // Create a table to store data for the DropDownList control. DataTable dt = new DataTable(); // Define the columns of the table. dt.Columns.Add(new DataColumn("ColorTextField", typeof(String))); dt.Columns.Add(new DataColumn("ColorValueField", typeof(String))); // Populate the table with sample values. dt.Rows.Add(CreateRow("White", "White", dt)); dt.Rows.Add(CreateRow("Silver", "Silver", dt)); dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt)); dt.Rows.Add(CreateRow("Khaki", "Khaki", dt)); dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt)); // Create a DataView from the DataTable to act as the data source // for the DropDownList control. DataView dv = new DataView(dt); return dv; } DataRow CreateRow(String Text, String Value, DataTable dt) { // Create a DataRow using the DataTable defined in the // CreateDataSource method. DataRow dr = dt.NewRow(); // This DataRow contains the ColorTextField and ColorValueField // fields, as defined in the CreateDataSource method. Set the // fields with the appropriate value. Remember that column 0 // is defined as ColorTextField, and column 1 is defined as // ColorValueField. dr[0] = Text; dr[1] = Value; return dr; } </script> <head runat="server"> <title> DropDownList Data Binding Example </title> </head> <body> <form id="form1" runat="server"> <h3> DropDownList Data Binding Example </h3> Select a background color for days in the calendar. <br /><br /> <asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server"/> <br /><br /> <table cellpadding="5"> <tr> <td> Background color: </td> </tr> <tr> <td> <asp:DropDownList id="ColorList" AutoPostBack="True" OnSelectedIndexChanged="Selection_Change" runat="server"/> </td> </tr> </table> </form> </body> </html>
- AspNetHostingPermission
Para trabajar en un entorno de host. Valor de la petición: LinkDemand; valor del permiso: Minimal
- AspNetHostingPermission
Para trabajar en un entorno de host. Valor de la petición: InheritanceDemand; valor del permiso: Minimal
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.