Export (0) Print
Expand All

ListItem.Selected Property

Gets or sets a value indicating whether the item is selected.

[Visual Basic]
Public Property Selected As Boolean
[C#]
public bool Selected {get; set;}
[C++]
public: __property bool get_Selected();
public: __property void set_Selected(bool);
[JScript]
public function get Selected() : Boolean;
public function set Selected(Boolean);

Property Value

true if the item is selected; otherwise, false. The default is false.

Remarks

Use the Selected property to determine whether the ListItem is currently selected in the list control that contains it. This property is commonly used, when iterating through a collection of ListItem objects in a list control, to determine whether an item is selected.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the Selected property when iterating through the Items collection of a CheckBoxList control to determine which check boxes are selected.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
 
 </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 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>
    

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
 
 </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 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>
    

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
 <head>
 
 </head>
 <body>
    <script language="JSCRIPT" runat="server">
       function Check_Clicked(sender : Object, e : EventArgs){
          Message.Text = "Selected Item(s):<br><br>"
          for(var i = 0; i < checkboxlist1.Items.Count; i++){
             if(checkboxlist1.Items(i).Selected)
                Message.Text = Message.Text + checkboxlist1.Items(i).Text + "<br>"
          }
       }
    </script>
 
    <form 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>
    

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript 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

ListItem Class | ListItem Members | System.Web.UI.WebControls Namespace | Items | CheckBoxList

Show:
© 2014 Microsoft