DropDownList 类

表示允许用户从下拉列表中选择一项的控件。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<ValidationPropertyAttribute("SelectedItem")> _
Public Class DropDownList
    Inherits ListControl
    Implements IPostBackDataHandler
用法
Dim instance As DropDownList
[ValidationPropertyAttribute("SelectedItem")] 
public class DropDownList : ListControl, IPostBackDataHandler
[ValidationPropertyAttribute(L"SelectedItem")] 
public ref class DropDownList : public ListControl, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */ 
public class DropDownList extends ListControl implements IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") 
public class DropDownList extends ListControl implements IPostBackDataHandler

备注

使用 DropDownList 控件可以创建只允许从中选择一项的下拉列表控件。可以通过设置 BorderColorBorderStyleBorderWidth 属性来控制 DropDownList 控件的外观。

若要指定希望显示在 DropDownList 控件中的项,请在 DropDownList 控件的开始和结束标记之间为每个项放置一个 ListItem 对象。

DropDownList 控件也支持数据绑定。若要将控件绑定到数据源,请创建一个包含要显示在控件中的项的数据源,如 System.Collections.ArrayList 对象。然后使用 Control.DataBind 方法将该数据源绑定到 DropDownList 控件。

使用 SelectedIndex 属性以编程方式确定用户从 DropDownList 控件中选择的项的索引。

辅助功能

默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用 FormView Web 服务器控件在网页中显示格式化数据 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 DropDownList Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Studio 中创建主/详细信息网页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 GridView Web 服务器控件中编辑时显示下拉列表 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 使用 Visual Web Developer 生成应用程序
演练:在 GridView Web 服务器控件中编辑时显示下拉列表 使用 Visual Web Developer 生成应用程序
演练:在 Visual Studio 中创建主/详细信息网页 使用 Visual Web Developer 生成应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 DropDownList Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用 FormView Web 服务器控件在网页中显示格式化数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:在 GridView Web 服务器控件中编辑时显示下拉列表 dv_vwdcon
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在非分层 Web 服务器控件中显示站点地图数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 DropDownList Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用 FormView Web 服务器控件在网页中显示格式化数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在非分层 Web 服务器控件中显示站点地图数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 DropDownList Web 服务器控件 生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序
如何:从数据源填充 List Web 服务器控件 生成 ASP .NET Web 应用程序
如何:在不分层的 Web 服务器控件中显示站点地图数据 生成 ASP .NET Web 应用程序
如何:在 List Web 服务器控件中添加项 生成 ASP .NET Web 应用程序
如何:设置 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序

示例

下面的代码示例演示如何创建包含四个项的 DropDownList 控件。

<%@ 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>
 
<%@ Page Language="C#" AutoEventWireup="True" %>

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

下面的代码示例演示如何通过数据绑定创建 DropDownList 控件。

<%@ 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>
 
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

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

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.BaseDataBoundControl
         System.Web.UI.WebControls.DataBoundControl
           System.Web.UI.WebControls.ListControl
            System.Web.UI.WebControls.DropDownList

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DropDownList 成员
System.Web.UI.WebControls 命名空间
BorderColor
BorderStyle
BorderWidth
ListItem
System.Collections.ArrayList
Control.DataBind
SelectedIndex