Bagikan melalui


ListBox.SelectionMode Properti

Definisi

Mendapatkan atau mengatur mode ListBox pemilihan kontrol.

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

Nilai Properti

Salah ListSelectionMode satu nilai. Nilai defaultnya adalah Single.

Pengecualian

Mode pemilihan yang ditentukan bukan salah ListSelectionMode satu nilai.

Contoh

Contoh berikut mengilustrasikan cara menggunakan SelectionMode properti untuk memungkinkan pengguna memilih beberapa pilihan dari ListBox kontrol.


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

Keterangan

SelectionMode Gunakan properti untuk menentukan perilaku ListBox mode kontrol. Mengatur properti ini untuk ListSelectionMode.Single menunjukkan hanya satu item yang dapat dipilih dari ListBox kontrol, sementara ListSelectionMode.Multiple menentukan beberapa item yang dapat dipilih.

Nilai SelectionMode properti disimpan dalam status tampilan.

Berlaku untuk

Lihat juga