次の方法で共有


ListBox.SelectionMode プロパティ

ListBox コントロールの選択モードを取得または設定します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Overridable Property SelectionMode As ListSelectionMode
'使用
Dim instance As ListBox
Dim value As ListSelectionMode

value = instance.SelectionMode

instance.SelectionMode = value
public virtual ListSelectionMode SelectionMode { get; set; }
public:
virtual property ListSelectionMode SelectionMode {
    ListSelectionMode get ();
    void set (ListSelectionMode value);
}
/** @property */
public ListSelectionMode get_SelectionMode ()

/** @property */
public void set_SelectionMode (ListSelectionMode value)
public function get SelectionMode () : ListSelectionMode

public function set SelectionMode (value : ListSelectionMode)
適用できません。

プロパティ値

ListSelectionMode 値の 1 つ。既定値は Single です。

例外

例外の種類 条件

ArgumentOutOfRangeException

指定された選択モードが ListSelectionMode 値ではありません。

解説

SelectionMode プロパティを使用して、ListBox コントロールのモード動作を指定します。このプロパティを ListSelectionMode.Single に設定した場合、ListBox コントロールで選択できるのは 1 つの項目だけですが、ListSelectionMode.Multiple に設定すると複数の項目を選択できます。

SelectionMode プロパティの値はビューステートに格納されます。

トピック 場所
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) Visual Studio での ASP .NET Web アプリケーションの作成
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
方法 : Web フォーム ページに ListBox Web サーバー コントロールを追加する ASP .NET Web アプリケーションの作成

使用例

SelectionMode プロパティを使用して、ユーザーが ListBox コントロールで複数の項目を選択できるようにする方法を次の例に示します。

<%@ 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 runat="server">
    <title>ListBox Example</title>
<script runat="server">

      Sub SubmitBtn_Click(sender As Object, e As EventArgs) 

         Message.Text = "You chose: <br />"
         
         ' Iterate through the Items collection of the ListBox and 
         ' display the selected items.
         Dim item As ListItem
         For Each item in ListBox1.Items

            If item.Selected Then

               Message.Text &= item.Text & "<br />"

            End If

         Next

      End Sub

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form id="form1" runat="server">

      Select items from the list and click Submit. <br />

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Multiple" 
           runat="server">

         <asp:ListItem Selected="True">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>

      <br /><br />

      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />

      <br /><br />
        
      <asp:Label id="Message" 
           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 runat="server">
    <title>ListBox Example</title>
<script runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {

         Message.Text = "You chose: <br />";
         
         // Iterate through the Items collection of the ListBox and 
         // display the selected items.
         foreach (ListItem item in ListBox1.Items)
         {

            if(item.Selected)
            {

               Message.Text += item.Text + "<br />";

            }

         }

      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form id="form1" runat="server">

      Select items from the list and click Submit. <br />

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Multiple" 
           runat="server">

         <asp:ListItem Selected="True">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>

      <br /><br />

      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />

      <br /><br />
        
      <asp:Label id="Message" 
           runat="server"/>
        
   </form>

</body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

ListBox クラス
ListBox メンバ
System.Web.UI.WebControls 名前空間
ListSelectionMode

その他の技術情報

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