This documentation is archived and is not being maintained.

ListBox.SelectionMode Property

Gets or sets the selection mode of the ListBox control.

[Visual Basic]
Public Overridable Property SelectionMode As ListSelectionMode
[C#]
public virtual ListSelectionMode SelectionMode {get; set;}
[C++]
public: __property virtual ListSelectionMode get_SelectionMode();
public: __property virtual void set_SelectionMode(ListSelectionMode);
[JScript]
public function get SelectionMode() : ListSelectionMode;
public function set SelectionMode(ListSelectionMode);

Property Value

One of the ListSelectionMode values. The default value is Single.

Exceptions

Exception Type Condition
ArgumentException The specified selection mode is not one of the ListSelectionMode values.

Remarks

Use the SelectionMode property to specify the mode behavior of the ListBox control. Setting this property to ListSelectionMode.Single indicates only a single item can be selected from the ListBox control, while ListSelectionMode.Multiple specifies multiple items can be selected.

Example

[Visual Basic, C#] The following example illustrates how to use the SelectionMode property to allow the user to select multiple selections from the ListBox control.

[Visual Basic] 

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

   <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

      Sub Selection_Change(sender As Object, e As EventArgs) 

         ' A single-selection ListBox cannot have multiple items selected. 
         ' Make sure only one item is selected before changing selection modes.
         ListBox1.SelectedIndex = 0

         ' Set the selection mode.
         ListBox1.SelectionMode = CType(SelectionModeList.SelectedIndex, ListSelectionMode)

      End Sub

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form runat=server>

      Select items from the list and click Submit. <br>
      Choose the selection mode from the drop-down list. <br>

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           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>

      Selection mode:

      <br>

      <asp:DropDownList id="SelectionModeList"
           AutoPostBack="True"
           OnSelectedIndexChanged="Selection_Change"
           runat="server">

         <asp:ListItem Selected="True">Single</asp:ListItem>
         <asp:ListItem>Multiple</asp:ListItem>

      </asp:DropDownList>

      <br><br>


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

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

</body>
</html>


[C#] 

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

   <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>";

            }

         }

      }

      void Selection_Change(Object sender, EventArgs e) 
      {

         // A single-selection ListBox cannot have multiple items selected. 
         // Make sure only one item is selected before changing selection modes.
         ListBox1.SelectedIndex = 0;

         // Set the selection mode.
         ListBox1.SelectionMode = (ListSelectionMode)SelectionModeList.SelectedIndex;

      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form runat=server>

      Select items from the list and click Submit. <br>
      Choose the selection mode from the drop-down list. <br>

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           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>

      Selection mode:

      <br>

      <asp:DropDownList id="SelectionModeList"
           AutoPostBack="True"
           OnSelectedIndexChanged="Selection_Change"
           runat="server">

         <asp:ListItem Selected="True">Single</asp:ListItem>
         <asp:ListItem>Multiple</asp:ListItem>

      </asp:DropDownList>

      <br><br>


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

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

</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

ListBox Class | ListBox Members | System.Web.UI.WebControls Namespace | ListSelectionMode

Show: