Gets or sets a value indicating whether the item is selected.
Assembly: System.Web (in System.Web.dll)
Public Property Selected As Boolean
public bool Selected { get; set; }
public: property bool Selected { bool get (); void set (bool value); }
member Selected : bool with get, set
Property Value
Type: System.Booleantrue if the item is selected; otherwise, false. The default is false.
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.
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.
Note
|
|---|
|
The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model. |
<%@ 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>
<%@ 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>
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note