ListItemCollection Class
TOC
Collapse the table of content
Expand the table of content

ListItemCollection Class

A collection of ListItem objects in a list control. This class cannot be inherited.

For a list of all members of this type, see ListItemCollection Members.

System.Object
   System.Web.UI.WebControls.ListItemCollection

[Visual Basic]
NotInheritable Public Class ListItemCollection
   Implements IList, ICollection, IEnumerable, IStateManager
[C#]
public sealed class ListItemCollection : IList, ICollection,
   IEnumerable, IStateManager
[C++]
public __gc __sealed class ListItemCollection : public IList,
   ICollection, IEnumerable, IStateManager
[JScript]
public class ListItemCollection implements IList, ICollection,
   IEnumerable, IStateManager

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The ListItemCollection class represents a collection of ListItem objects. The ListItem objects, in turn, represent the items displayed in list controls, such as the ListBox. To programmatically retrieve ListItem objects from a list control, use one of following methods:

  • Use the indexer to get a single ListItem from the collection, using array notation.
  • Use the CopyTo method to copy the contents of the collection to a System.Array object, which can then be used to get items from the collection.
  • Use the GetEnumerator method to create a System.Collections.IEnumerator implemented object, which can then be used to get items from the collection.

The Count property specifies the total number of items in the collection, and is commonly used to determine the upper bound of the collection. You can add and remove items from the collection by using the Add and Remove methods.

Example

[Visual Basic] 
<!-- This example demonstrates creating ListItemCollection objects, 
adding items to the collections, and removing items from the collections. -->
    . . . 
<!-- In the example, listBoxData ListItemCollection is used as the 
data source for a ListBox control called ListBox1, and the ddBoxData 
ListItemCollection is used as the data source for a DropDownList 
control called DropDownList1. --> 
    . . . 
<%@ Page language="VB" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        <SCRIPT runat="server">
            Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
                ' If this isn't the first time the page is loaded in this session,
                ' then don't recreate the DataTables.
                If Not IsPostBack Then
                    ' Create a new ListItemCollection.
                    Dim listBoxData As New ListItemCollection()
                    ' Add items to the collection.
                    listBoxData.Add(New ListItem("apples"))
                    listBoxData.Add(New ListItem("bananas"))
                    listBoxData.Add(New ListItem("cherries"))
                    listBoxData.Add("grapes")
                    listBoxData.Add("mangos")
                    listBoxData.Add("oranges")
                    ' Set the ListItemCollection as the data source for ListBox1.
                    ListBox1.DataSource = listBoxData
                    ListBox1.DataBind()
         
                    ' Create a new ListItemCollection.
                    Dim ddBoxData As New ListItemCollection()
                    ' For now, just bind the data to the DropDownList.
                    DropDownList1.DataSource = ddBoxData
                    DropDownList1.DataBind()
                End If
            End Sub 'Page_Load

            Private Sub moveButton1_Click(sender As Object, e As System.EventArgs) 
                'Set the SelectedIndex to -1 so no items are selected.            
                ' The new item will be set as the selected item when it is added.
                DropDownList1.SelectedIndex = - 1
                ' Add the selected item to DropDownList1.
                DropDownList1.Items.Add(ListBox1.SelectedItem)
                ' Delete the selected item from ListBox1.
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            End Sub 'moveButton1_Click
   
   
            Private Sub moveButton2_Click(sender As Object, e As System.EventArgs)
                'Set the SelectedIndex to -1 so no items are selected.
                ' The new item will be set as the selected item when it is added.
                ListBox1.SelectedIndex = - 1
                ' Add the selected item to ListBox1.
                ListBox1.Items.Add(DropDownList1.SelectedItem)
                'Delete the selected item from DropDownList1.
                DropDownList1.Items.Remove(DropDownList1.SelectedItem)
            End Sub 'moveButton2_Click
        </SCRIPT>
    </HEAD>

    <BODY>
        <form runat="server">
            <table cellpadding="6" border="0">
                <tr>
                    <td rowspan="2" valign="top">
                        <asp:ListBox id="ListBox1" runat="server" 
                            SelectionMode="Single" Width="100px">
                        </asp:ListBox>
                    </td>

                    <td>
                        <asp:Button id="moveButton1" runat="server" Text="Move -->"
                            Width="100px" OnClick="moveButton1_Click"></asp:Button>
                    </td>

                    <td rowspan="2" valign="top">
                        <asp:DropDownList Runat="server" ID="DropDownList1"
                            Width="100px">
                        </asp:DropDownList>
                    </td>
                </tr>

                <tr>
                    <td>
                        <asp:Button ID="moveButton2" Runat="server" Text="<-- Move"
                            Width="100px" onClick="moveButton2_Click"></asp:Button>
                    </td>
                </tr>
            </table>
        </form>
    </BODY>
</HTML>

[C#] 
<!-- This example demonstrates creating ListItemCollection objects, 
adding items to the collections, and removing items from the collections. -->
    . . . 
<!-- In the example, listBoxData ListItemCollection is used as the data 
source for a ListBox control called ListBox1, and the ddBoxData 
ListItemCollection is used as the data source for a DropDownList 
control called DropDownList1. --> 
    . . . 
<%@ Page language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        <SCRIPT runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
                // If this isn't the first time the page is loaded in this session,
                // then don't recreate the DataTables.
                if (!IsPostBack)
                {
                    // Create a new ListItemCollection.
                    ListItemCollection listBoxData = new ListItemCollection();
                    // Add items to the collection.
                    listBoxData.Add(new ListItem("apples"));
                    listBoxData.Add(new ListItem("bananas"));
                    listBoxData.Add(new ListItem("cherries"));
                    listBoxData.Add("grapes");
                    listBoxData.Add("mangos");
                    listBoxData.Add("oranges");
                    // Set the ListItemCollection as the data source for ListBox1.
                    ListBox1.DataSource = listBoxData;
                    ListBox1.DataBind();

                    // Create a new ListItemCollection.
                    ListItemCollection ddBoxData = new ListItemCollection();
                    // For now, just bind the data to the DropDownList.
                    DropDownList1.DataSource = ddBoxData;
                    DropDownList1.DataBind();
                }        
            }

            private void moveButton1_Click(object sender, System.EventArgs e)
            {
                //Set the SelectedIndex to -1 so no items are selected.
                // The new item will be set as the selected item when it is added.
                DropDownList1.SelectedIndex = -1;
                // Add the selected item to DropDownList1.
                DropDownList1.Items.Add(ListBox1.SelectedItem);
                // Delete the selected item from ListBox1.
                ListBox1.Items.Remove(ListBox1.SelectedItem);
            }

            private void moveButton2_Click(object sender, System.EventArgs e)
            {
                //Set the SelectedIndex to -1 so no items are selected.
                // The new item will be set as the selected item when it is added.
                ListBox1.SelectedIndex = -1;
                // Add the selected item to ListBox1.
                ListBox1.Items.Add(DropDownList1.SelectedItem);
                //Delete the selected item from DropDownList1.
                DropDownList1.Items.Remove(DropDownList1.SelectedItem);
            }
        </SCRIPT>
    </HEAD>

    <BODY>
        <form runat="server">
            <table cellpadding="6" border="0">
                <tr>
                    <td rowspan="2" valign="top">
                        <asp:ListBox id="ListBox1" runat="server" 
                            SelectionMode="Single" Width="100px">
                        </asp:ListBox>
                    </td>

                    <td>
                        <asp:Button id="moveButton1" runat="server" Text="Move -->"
                            Width="100px" OnClick="moveButton1_Click"></asp:Button>
                    </td>

                    <td rowspan="2" valign="top">
                        <asp:DropDownList Runat="server" ID="DropDownList1"
                            Width="100px">
                        </asp:DropDownList>
                    </td>
                </tr>

                <tr>
                    <td>
                        <asp:Button ID="moveButton2" Runat="server" Text="<-- Move"
                            Width="100px" onClick="moveButton2_Click"></asp:Button>
                    </td>
                </tr>
            </table>
        </form>
    </BODY>
</HTML>

[Visual Basic] 
<!-- This example demonstrates how to select multiple items from a DataList 
and add the selected items to a DataGrid. The example uses a For Each loop 
to iterate through the ListItem objects in the ListItemCollection of ListBox1. -->

    . . . 
<%@ Page language="VB" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        
        <SCRIPT runat="server">
            ' Global Variables.
            Private dv As DataView
            Private dt As New DataTable()

            Private Sub Page_Load(sender As Object, e As System.EventArgs)
                ' Set the number of rows displayed in the ListBox to be
                ' the number of items in the ListBoxCollection.
                ListBox1.Rows = ListBox1.Items.Count

                ' If the DataTable is already stored in the Web form's default
                ' HttpSessionState variable, then don't recreate the DataTable.
                If Session("data") Is Nothing Then
                    ' Add columns to the DataTable.
                    dt.Columns.Add(New DataColumn("Item"))
                    dt.Columns.Add(New DataColumn("Price"))
        
                    ' Store the DataTable in the Session variable so it can be 
                    ' accessed again later.
                    Session("data") = dt
                    
                    ' Use the table to create a DataView, because the DataGrid
                    ' can only bind to a data source that implements IEnumerable.
                    dv = New DataView(dt)
            
                    ' Set the DataView as the data source, and bind it to the DataGrid.
                    DataGrid1.DataSource = dv
                    DataGrid1.DataBind()
                End If
            End Sub

            Private Sub addButton_Click(sender As Object, e As System.EventArgs)
                ' Add the items selected in ListBox1 to DataGrid1.
                Dim item As ListItem
                For Each item In ListBox1.Items
                    If item.Selected Then
                        ' Add the item to the DataGrid.
                        ' First, get the DataTable from the Session variable.
                        dt = CType(Session("data"), DataTable)
            
                        If  Not (dt Is Nothing) Then
                            ' Create a new DataRow in the DataTable.
                            Dim dr As DataRow
                            dr = dt.NewRow()
                            ' Add the item to the new DataRow.
                            dr("Item") = item.Text
                            ' Add the item's value to the DataRow.
                            dr("Price") = item.Value
                            ' Add the DataRow to the DataTable.
                            dt.Rows.Add(dr)

                            ' Rebind the data to DataGrid1.
                            dv = new DataView(dt)
                            DataGrid1.DataSource = dv
                            DataGrid1.DataBind()
                        End If
                    End If
                Next item
            End Sub
        </SCRIPT>

    </HEAD>
    
    <BODY>
        <form runat="server">

            <h3> ListItemCollection Example </h3>

            <table cellpadding="6" border="0">
                <tr>
                    <td valign="top">
                        <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
                            <asp:ListItem Value=".89">apples</asp:ListItem>
                            <asp:ListItem Value=".49">bananas</asp:ListItem>
                            <asp:ListItem Value="2.99">cherries</asp:ListItem>
                            <asp:ListItem Value="1.49">grapes</asp:ListItem>
                            <asp:ListItem Value="2.00">mangos</asp:ListItem>
                            <asp:ListItem Value="1.09">oranges</asp:ListItem>
                        </asp:ListBox>
                    </td>

                    <td valign="top">
                        <asp:Button id="addButton" runat="server" Text="Add -->"
                            Width="100px" OnClick="addButton_Click"></asp:Button>
                    </td>

                    <td valign="top">
                        <asp:DataGrid Runat="server" ID="DataGrid1" CellPadding="4">
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>        
        </form>
    </body>
</HTML>

[C#] 
<!-- This example demonstrates how to select multiple items from a DataList and add the 
selected items to a DataGrid. The example uses a foreach loop to iterate through 
the ListItem objects in the ListItemCollection of ListBox1. -->

    . . . 
<%@ Page language="c#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        
        <SCRIPT language="C#" runat="server">
            // Global Variables.
            private DataView dv;
            private DataTable dt = new DataTable();

            private void Page_Load(object sender, System.EventArgs e)
            {
                // Set the number of rows displayed in the ListBox to be
                // the number of items in the ListBoxCollection.
                ListBox1.Rows = ListBox1.Items.Count;

                // If the DataTable is already stored in the Web form's default
                // HttpSessionState variable, then don't recreate the DataTable.
                if (Session["data"] == null)
                {
                    // Add columns to the DataTable.
                    dt.Columns.Add(new DataColumn("Item"));
                    dt.Columns.Add(new DataColumn("Price"));
        
                    // Store the DataTable in the Session variable so it can 
                    // be accessed again later.
                    Session["data"] = dt;
                    
                    // Use the table to create a DataView, because the DataGrid
                    // can only bind to a data source that implements IEnumerable.
                    dv = new DataView(dt);
            
                    // Set the DataView as the data source, and bind it to the DataGrid.
                    DataGrid1.DataSource = dv;
                    DataGrid1.DataBind();
                }
            }

            private void addButton_Click(object sender, System.EventArgs e)
            {
                // Add the items selected in ListBox1 to DataGrid1.
                foreach (ListItem item in ListBox1.Items)
                {
                    if (item.Selected)
                    {
                        // Add the item to the DataGrid.
                        // First, get the DataTable from the Session variable.
                        dt = (DataTable)Session["data"];
            
                        if (dt != null)
                        { 
                            // Create a new DataRow in the DataTable.
                            DataRow dr = dt.NewRow();
                            // Add the item to the new DataRow.
                            dr["Item"] = item.Text;
                            // Add the item's value to the DataRow.
                            dr["Price"] = item.Value;
                            // Add the DataRow to the DataTable.
                            dt.Rows.Add(dr);

                            // Rebind the data to DataGrid1.
                            dv = new DataView(dt);
                            DataGrid1.DataSource = dv;
                            DataGrid1.DataBind();
                        }
                    }
                }
            }
        </SCRIPT>

    </HEAD>
    
    <BODY>
        <form runat="server">

            <h3> ListItemCollection Example </h3>

            <table cellpadding="6" border="0">
                <tr>
                    <td valign="top">
                        <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
                            <asp:ListItem Value=".89">apples</asp:ListItem>
                            <asp:ListItem Value=".49">bananas</asp:ListItem>
                            <asp:ListItem Value="2.99">cherries</asp:ListItem>
                            <asp:ListItem Value="1.49">grapes</asp:ListItem>
                            <asp:ListItem Value="2.00">mangos</asp:ListItem>
                            <asp:ListItem Value="1.09">oranges</asp:ListItem>
                        </asp:ListBox>
                    </td>

                    <td valign="top">
                        <asp:Button id="addButton" runat="server" Text="Add -->"
                            Width="100px" OnClick="addButton_Click"></asp:Button>
                    </td>

                    <td valign="top">
                        <asp:DataGrid Runat="server" ID="DataGrid1" CellPadding="4">
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>        
        </form>
    </body>
</HTML>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

ListItemCollection Members | System.Web.UI.WebControls Namespace | ListItem | ListBox | CopyTo | GetEnumerator | System.Collections.IEnumerator | Count | Add | Remove

Show:
© 2016 Microsoft