DropDownList クラス
アセンブリ: System.Web (system.web.dll 内)

<ValidationPropertyAttribute("SelectedItem")> _ Public Class DropDownList Inherits ListControl Implements IPostBackDataHandler
[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 コントロールを使用して、単一選択ドロップダウン リスト コントロールを作成します。BorderColor、BorderStyle、BorderWidth の各プロパティを設定すると DropDownList コントロールの外観を制御できます。
DropDownList コントロールに表示する項目を指定するには、各エントリに対して、DropDownList コントロールの開始タグと終了タグの間に ListItem オブジェクトを配置します。
DropDownList コントロールはデータ連結もサポートしています。コントロールをデータ ソースにバインドするには、コントロールに表示する項目を格納している System.Collections.ArrayList オブジェクトなどのデータ ソースを作成します。データ ソースを作成したら、Control.DataBind メソッドを使用してデータ ソースを DropDownList コントロールに連結します。
SelectedIndex プロパティを使用して、DropDownList コントロールからユーザーが選択した項目のインデックスをプログラムにより確認します。
ユーザー補助このコントロールに既定でレンダリングされるマークアップは、Web Content Accessibility Guidelines (WCAG) 1.0 の優先度 1 ガイドラインなどのユーザー補助に関する標準に適合しない可能性があります。このコントロールのユーザー補助サポートの詳細については、「ASP.NET コントロールとユーザー補助」を参照してください。

4 つの項目が格納されている 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>


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


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DropDownList メンバ
System.Web.UI.WebControls 名前空間
BorderColor
BorderStyle
BorderWidth
ListItem
System.Collections.ArrayList
Control.DataBind
SelectedIndex
Weblioに収録されているすべての辞書からDropDownList クラスを検索する場合は、下記のリンクをクリックしてください。

- DropDownList クラスのページへのリンク