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

単一選択または複数選択のリスト ボックスを作成します。

<asp:ListBoxid="Listbox1"      DataSource="<% databindingexpression %>"     DataTextField="DataSourceField"     DataValueField="DataSourceField"     AutoPostBack="True|False"     Rows="rowcount"     SelectionMode="Single|Multiple"     OnSelectedIndexChanged="OnSelectedIndexChangedMethod"     runat="server">   <asp:ListItemvalue="value" selected="True|False">      Text   </asp:ListItem></asp:ListBox>

解説

ListBox コントロールを使用して、単一または複数の項目を選択できるリスト コントロールを作成できます。Rows プロパティを使用して、コントロールの高さを指定します。複数の項目を選択できるようにするには、SelectionMode プロパティに ListSelectionMode.Multiple を設定します。

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

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

SelectionMode プロパティに ListSelectionMode.Multiple を設定した場合は、Items コレクションを反復処理し、コレクション内の各項目の Selected プロパティを調べて、ListBox コントロールで選択されている項目を判断します。SelectionMode プロパティに ListSelectionMode.Single を設定した場合は、SelectedIndex プロパティを使用して、選択されている項目のインデックスを判断できます。次に、このインデックスを使用して、項目を Items コレクションから取得します。

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

ListBox コントロールを使用して、定義済みオプションのリストを表示する方法を次の例に示します。ユーザーが選択した項目が、Label コントロールに表示されます。

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub SubmitBtn_Click(sender As Object, e As EventArgs)
         If ListBox1.SelectedIndex > - 1 Then
            Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
         End If
      End Sub 'SubmitBtn_Click
   </script>
</head>
<body>
   <h3>ListBox Example</h3>
   <p>
   <form runat="server">
      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           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:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>
      </asp:ListBox>
      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />
      <p>
      <asp:Label id="Label1" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         if (ListBox1.SelectedIndex > -1)
            Label1.Text="The first item you chose: " +
                        ListBox1.SelectedItem.Text;
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>ListBox Example</h3>
      <asp:ListBox id="ListBox1"  
           Rows="4" 
           SelectionMode="Multiple" 
           Width="100px" 
           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:ListItem>Item 5</asp:ListItem> 
         <asp:ListItem>Item 6</asp:ListItem>
      </asp:ListBox>
      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />
      <p>
      <asp:Label id="Label1" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
   </form>
</body>
</html>

参照

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