DropDownList Web サーバー コントロール

ユーザーが単一選択ドロップダウン リストから項目を選択できるようにします。ドロップダウン リストに含めることができる項目の数に制限はありません。

<asp:DropDownListid="DropDownList1" runat="server"     DataSource="<% databindingexpression %>"     DataTextField="DataSourceField"     DataValueField="DataSourceField"     AutoPostBack="True|False"     OnSelectedIndexChanged="OnSelectedIndexChangedMethod">   <asp:ListItemvalue="value" selected="True|False">      Text   </asp:ListItem></asp:DropDownList>

解説

DropDownList コントロールを使用して、単一選択ドロップダウン リスト コントロールを作成できます。DropDownList コントロールの外観を制御するには、BorderColorBorderStyleBorderWidth の各プロパティを設定します。

DropDownList コントロールに表示する項目を指定するには、DropDownList コントロールの開始タグと終了タグの間に、各エントリの ListItem 要素を挿入します。

DropDownList コントロールは、データ連結もサポートします。コントロールをデータ ソースに連結するには、まずコントロールに表示する項目を含む System.Collections.ArrayList などのデータ ソースを作成します。次に、Control.DataBind メソッドを使用して、そのデータ ソースを DropDownList コントロールに連結します。DataTextField プロパティと DataValueField プロパティを使用して、コントロール内の各リスト項目の Text プロパティと Value プロパティに連結するデータ ソース フィールドをそれぞれ指定します。これで、DropDownList コントロールに、データ ソースの情報が表示されるようになります。

SelectedIndex プロパティを使用して、ユーザーが DropDownList コントロールから選択した項目のインデックスをプログラムによって判断します。次に、このインデックスを使用して、選択されている項目をコントロールの Items コレクションから取得します。

DropDownList Web サーバー コントロールのプロパティとイベントの詳細については、DropDownList のドキュメントを参照してください。

DropDownList コントロールの使用方法を次の例に示します。

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Button_Click(sender As Object, e As EventArgs)
         Label1.Text = "You selected " & _
                       DropDownList1.SelectedItem.Text & "."
      End Sub 'Button_Click
   </script>
</head>
<body>
   <form runat="server">
      <h3>DropDownList Example</h3>
      Select an item from the list and click the submit button.
      <p>
      <asp:DropDownList id="DropDownList1" 
           runat="server">
         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem> 
      </asp:DropDownList>
      <br><br>
      <asp:Button id="Button1" 
           Text="Submit" 
           OnClick="Button_Click" 
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void Button_Click(Object sender, EventArgs e) 
      {
         Label1.Text = "You selected: " + 
         dropdownlist1.SelectedItem.Text + ".";         
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>DropDownList Example</h3>
      Select an item from the list and click the submit button.
      <p>
      <asp:DropDownList id="dropdownlist1" runat="server">
         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
      </asp:DropDownList>
      <br><br>
      <asp:Button id="Button1"  
           Text="Submit" 
           OnClick="Button_Click" 
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" runat="server"/>
   </form>
</body>
</html>

参照

Web サーバー コントロール | DropDownList クラス