DropDownList Class
Assembly: System.Web (in system.web.dll)
'Declaration <ValidationPropertyAttribute("SelectedItem")> _ Public Class DropDownList Inherits ListControl Implements IPostBackDataHandler 'Usage Dim instance As DropDownList
/** @attribute ValidationPropertyAttribute("SelectedItem") */
public class DropDownList extends ListControl implements IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") public class DropDownList extends ListControl implements IPostBackDataHandler
Use the DropDownList control to create a single-selection drop-down list control. You can control the appearance of the DropDownList control by setting the BorderColor, BorderStyle, and BorderWidth properties.
To specify the items that you want to appear in the DropDownList control, place a ListItem object for each entry between the opening and closing tags of the DropDownList control.
The DropDownList control also supports data binding. To bind the control to a data source, create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the control. Then, use the Control.DataBind method to bind the data source to the DropDownList control.
Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control.
Accessibility
The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.
The following code example demonstrates how to create a DropDownList control that contains four items.
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <script runat="server" > Sub Selection_Change(sender As Object, e As EventArgs) ' 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) End Sub </script> <body> <form 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> </form> </body> </html>
The following code example demonstrates how to create a DropDownList control though data binding.
<%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <script runat="server" > Sub Selection_Change(sender as Object, e As EventArgs) ' 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) End Sub Sub Page_Load(sender as Object, e As EventArgs) ' Load data for the DropDownList control only once, when the ' page is first loaded. If Not IsPostBack Then ' 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 End If End Sub Function CreateDataSource() As ICollection ' Create a table to store data for the DropDownList control. Dim dt As DataTable = New DataTable() ' Define the columns of the table. dt.Columns.Add(new DataColumn("ColorTextField", GetType(String))) dt.Columns.Add(new DataColumn("ColorValueField", GetType(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. Dim dv As DataView = New DataView(dt) Return dv End Function Function CreateRow(Text As String, Value As String, dt As DataTable) As DataRow ' Create a DataRow using the DataTable defined in the ' CreateDataSource method. Dim dr As DataRow = 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 End Function </script> <body> <form 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> </form> </body> </html>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.