.NET Framework Class Library
ListControl..::.Items Property

Gets the collection of items in the list control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
<PersistenceModeAttribute(PersistenceMode.InnerDefaultProperty)> _
Public Overridable ReadOnly Property Items As ListItemCollection
Visual Basic (Usage)
Dim instance As ListControl
Dim value As ListItemCollection

value = instance.Items
C#
[PersistenceModeAttribute(PersistenceMode.InnerDefaultProperty)]
public virtual ListItemCollection Items { get; }
Visual C++
[PersistenceModeAttribute(PersistenceMode::InnerDefaultProperty)]
public:
virtual property ListItemCollection^ Items {
    ListItemCollection^ get ();
}
JScript
public function get Items () : ListItemCollection

Property Value

Type: System.Web.UI.WebControls..::.ListItemCollection
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.

TopicLocation
How to: Set Web Server Control Properties in CollectionsBuilding ASP .NET Web Applications
How to: Set Web Server Control Properties in CollectionsBuilding ASP .NET Web Applications
How to: Set Web Server Control Properties in CollectionsBuilding ASP .NET Web Applications in Visual Studio
How to: Set Web Server Control Properties in CollectionsBuilding ASP .NET Web Applications in Visual Studio
Examples

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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head runat="server">
    <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" 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head runat="server">
    <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" 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head runat="server">
    <title>ASP.NET Example</title>
</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 id="form1" 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>

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker