ListItem.Selected Özellik

Tanım

Öğenin seçili olup olmadığını belirten bir değer alır veya ayarlar.

public:
 property bool Selected { bool get(); void set(bool value); };
public bool Selected { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.MinimizableAttributeTypeConverter))]
public bool Selected { get; set; }
member this.Selected : bool with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.MinimizableAttributeTypeConverter))>]
member this.Selected : bool with get, set
Public Property Selected As Boolean

Özellik Değeri

true öğe seçiliyse; aksi takdirde , false. Varsayılan değer: false.

Öznitelikler

Örnekler

Aşağıdaki örnek, hangi onay kutularının Selected seçildiğini belirlemek için bir CheckBoxList denetimin Items koleksiyonunda yineleme yaparken özelliğinin nasıl kullanılacağını gösterir.

Not

Aşağıdaki kod örneği tek dosyalı kod modelini kullanır ve doğrudan arka planda kod dosyasına kopyalandığında düzgün çalışmayabilir. Bu kod örneği.aspx uzantısına sahip boş bir metin dosyasına kopyalanmalıdır. Web Forms kod modeli hakkında daha fazla bilgi için bkz. ASP.NET Web Forms Sayfa Kodu Modeli.

<%@ 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>ASP.NET Example</title>
</head>
 <body>
    <script language="C#" runat="server">
       void Check_Clicked(Object sender, EventArgs e) {
          Message.Text="Selected Item(s):" + "<br />" + "<br />";
          for (int i=0; i<checkboxlist1.Items.Count; i++) {
             if (checkboxlist1.Items[i].Selected)
                Message.Text=Message.Text + checkboxlist1.Items[i].Text + "<br />";
          }
       }
    </script>
 
    <form id="form1" action="checkboxlist.aspx" method="post" runat="server">
 
       <asp:CheckBoxList id="checkboxlist1" runat="server"
            AutoPostBack="True"
            CellPadding="5"
            CellSpacing="5"
            RepeatColumns="2"
            RepeatDirection="Vertical"
            RepeatLayout="Flow"
            TextAlign="Right"
            OnSelectedIndexChanged="Check_Clicked">
 
          <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:CheckBoxList>
 
       <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>
    <title>ASP.NET Example</title>
</head>
 <body>
    <script language="VB" runat="server">
       Sub Check_Clicked(sender As Object, e As EventArgs)
          Message.Text = "Selected Item(s):<br /><br />"
          Dim i As Integer
          For i = 0 To checkboxlist1.Items.Count - 1
             If checkboxlist1.Items(i).Selected Then
                Message.Text = Message.Text & checkboxlist1.Items(i).Text & "<br />"
             End If
          Next
       End Sub
    </script>
 
    <form id="form1" action="checkboxlist.aspx" method="post" runat="server">
 
       <asp:CheckBoxList id="checkboxlist1" runat="server"
            AutoPostBack="True"
            CellPadding="5"
            CellSpacing="5"
            RepeatColumns="2"
            RepeatDirection="Vertical"
            RepeatLayout="Flow"
            TextAlign="Right"
            OnSelectedIndexChanged="Check_Clicked">
 
          <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:CheckBoxList>
 
       <br /><br />
       <asp:label id="Message" runat="server"/>
             
    </form>
         
 </body>
 </html>

Açıklamalar

Selected özelliğini kullanarak öğesinin o anda onu içeren liste denetiminde seçili olup olmadığını ListItem belirleyin. Bu özellik, bir öğenin seçili olup olmadığını belirlemek için bir liste denetimindeki bir nesne koleksiyonunda ListItem yineleme yaparken yaygın olarak kullanılır.

Şunlara uygulanır

Ayrıca bkz.