ListControl.Items Property
Gets the collection of items in the list control.
[Visual Basic] Public Overridable ReadOnly Property Items As ListItemCollection [C#] public virtual ListItemCollection Items {get;} [C++] public: __property virtual ListItemCollection* get_Items(); [JScript] public function get Items() : ListItemCollection;
Property Value
A ListItemCollection that represents the items within the list. The default is an empty list.
Remarks
Use this property to get the properties of items in the list control. This property can be used to determine the selected items in the list control.
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the Items property to determine the selected items in a CheckBoxList control.
[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
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
ListControl Class | ListControl Members | System.Web.UI.WebControls Namespace | ListItemCollection