ListBox.SelectionMode 属性

定义

获取或设置 ListBox 控件的选择模式。

public:
 virtual property System::Web::UI::WebControls::ListSelectionMode SelectionMode { System::Web::UI::WebControls::ListSelectionMode get(); void set(System::Web::UI::WebControls::ListSelectionMode value); };
public virtual System.Web.UI.WebControls.ListSelectionMode SelectionMode { get; set; }
member this.SelectionMode : System.Web.UI.WebControls.ListSelectionMode with get, set
Public Overridable Property SelectionMode As ListSelectionMode

属性值

ListSelectionMode

ListSelectionMode 值之一。 默认值是 Single

例外

指定的选择模式不是 ListSelectionMode 值之一。

示例

下面的示例演示如何使用 SelectionMode 该属性允许用户从 ListBox 控件中选择多个选择。


<%@ 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>

<%@ 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>

注解

使用 SelectionMode 属性指定控件的 ListBox 模式行为。 将此属性设置为 ListSelectionMode.Single 指示只能从 ListBox 控件中选择单个项,同时 ListSelectionMode.Multiple 可以指定多个项。

属性的值 SelectionMode 存储在视图状态中。

适用于

另请参阅