次の方法で共有


ListBox Web サーバー コントロールの宣言構文

更新 : 2007 年 11 月

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

<asp:ListBox
    AccessKey="string"
    AppendDataBoundItems="True|False"
    AutoPostBack="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    CssClass="string"
    DataMember="string"
    DataSource="string"
    DataSourceID="string"
    DataTextField="string"
    DataTextFormatString="string"
    DataValueField="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDataBound="DataBound event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedIndexChanged="SelectedIndexChanged event handler"
    OnTextChanged="TextChanged event handler"
    OnUnload="Unload event handler"
    Rows="integer"
    runat="server"
    SelectedIndex="integer"
    SelectedValue="string"
    SelectionMode="Single|Multiple"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
>
            <asp:ListItem
                Enabled="True|False"
                Selected="True|False"
                Text="string"
                Value="string"
            />
</asp:ListBox>

解説

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

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

ListBox コントロールは、データ連結もサポートします。コントロールをデータ ソースにバインドするには、まず、コントロールに表示する項目を格納する DataSourceControl オブジェクトなどのデータ ソースを作成します。次に、DataBind メソッドを使用して、作成したデータ ソースを ListBox コントロールにバインドします。DataTextField プロパティと DataValueField プロパティを使用して、コントロールの各リスト項目の Text プロパティと Value プロパティにバインドするデータ ソース フィールドをそれぞれ指定します。これで、ListBox コントロールに、データ ソースの情報が表示されるようになります。

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

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

使用例

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

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ListBox Example</title>
<script language="VB" 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>

   <form id="form1" 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" />

      <asp:Label id="Label1" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ListBox Example</title>
<script language="C#" runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         if (ListBox1.SelectedIndex > -1)
            Label1.Text="You chose: " + ListBox1.SelectedItem.Text;
      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form id="form1" 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" />

      <asp:Label id="Label1" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

   </form>

</body>
</html>

参照

参照

ListBox

その他の技術情報

Web サーバー コントロール構文