DataListItemCollection Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents the collection of DataListItem objects in the DataList control. This class cannot be inherited.

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


public sealed class DataListItemCollection : ICollection, IEnumerable


Initializes a new instance of the DataListItemCollection class.


Gets the number of DataListItem objects in the collection.


Gets a value that indicates whether the DataListItem objects in the DataListItemCollection can be modified.


Gets a value indicating whether access to the DataListItemCollection is synchronized (thread-safe).


Gets a DataListItem object at the specified index in the collection.


Gets the object that can be used to synchronize access to the DataListItemCollection collection.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies all the items from this DataListItemCollection collection to the specified System.Array object, starting at the specified index in the System.Array object.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Returns a System.Collections.IEnumerator interface that contains all DataListItem objects in the DataListItemCollection.


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The DataListItemCollection class represents a collection of DataListItem objects, which in turn represent the data items in a DataList control. To programmatically retrieve DataListItem objects from a DataList control, use one of the following methods:

  • Use the indexer to get a single DataListItem object 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 interface, 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.

The following example creates a data source in the form of a DataView control and binds it to a DataList control declared in markup. It then displays the contents of the underlying DataListItemCollection object on the page.

<%@ Import Namespace = "System.Data"  %>
<%@ Page language="c#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">    
// The following example creates a DataSource as a DataView.
// The DataView is bound to a DataList that is displayed using an
// ItemTemplate. When the page is first loaded, the program uses the 
// CopyTo method to copy the entire data source to an Array and then 
// displays that data.

ICollection CreateDataSource()
    DataTable myDataTable = new DataTable();
    DataRow myDataRow;

    myDataTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
    myDataTable.Columns.Add(new DataColumn("EmployeeID", typeof(long)));
    for (int i = 0; i < 3; i++)
        myDataRow = myDataTable.NewRow();
        myDataRow[0] = "somename" + i.ToString();
        myDataRow[1] = (i+1000);
    DataView dataView = new DataView(myDataTable);
    return dataView;

void Page_Load(Object sender, EventArgs e)
    if (!IsPostBack)
        // Bind the DataView to the DataSource.
        myDataList.DataSource = CreateDataSource();
        // Create a Array to hold the DataSource.
        System.Array myArray = Array.CreateInstance(typeof(DataListItem),
        // Copy the DataSource to an Array.

// Prints each element in the Array onto the label lblAllItems1.
public void PrintValues(Array myArr)
    DataListItem currentItem;
    System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
    while (myEnumerator.MoveNext())
        currentItem = (DataListItem)myEnumerator.Current;
        lblAllItems1.Text += "<br /><br />" + 

// Event handler method for show button.
void show_Click(Object sender,EventArgs e)
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.Items;
    // Display the read-only properties.
    Response.Write("<b>The Total number of items are " + 
        myDataListItemCollection.Count + "</b>");
    Response.Write("<br /><b>The ReadOnly property of the " + 
        "DataListItemCollection is always" + 
        myDataListItemCollection.IsReadOnly + "</b>");
    Response.Write("<br /><b>The IsSynchronized property of the " +
        "DataListItemCollection is always " +
        myDataListItemCollection.IsSynchronized + "</b>");
    myDataListItemCollection = null;

void allItems_Click(Object sender,EventArgs e)
    IEnumerator dataListEnumerator;
    DataListItem currentItem;
    lblAllItems.Text = "";
    // Get an enumerator to traverse the DataListItemCollection.
    dataListEnumerator = myDataList.Items.GetEnumerator();
        currentItem = (DataListItem)dataListEnumerator.Current;
        // Display the current DataListItem onto the label.
        lblAllItems.Text += ((Label)(currentItem.Controls[1])).Text + " ";

void itemSelected(Object sender,EventArgs e)
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.Items;
    // Get the index of the selected radio button in the RadioButtonList.
    int index = Convert.ToInt16(listItemNo.SelectedItem.Value);
    // Get the DataListItem corresponding to index from DataList.
    // SyncRoot is used to make access to the DataListItemCollection 
    // in a thread-safe manner It returns the object that invoked it. 
    DataListItem currentItem = 
    // Display the selected DataListItem onto a label.
    lblDisplay.Text = "<b>DataListItem" + index + " is : "
        + ((Label)(currentItem.Controls[1])).Text;
    currentItem = null;
    myDataListItemCollection = null;

<html xmlns="" >
        DataListItemCollection Example
<form runat="server" id="Form1">
        DataListItemCollection Example
                <asp:datalist id="myDataList" runat="server" Font-Size="8pt" 
                    Font-Names="Verdana" BorderColor="black" CellSpacing="5"
                    CellPadding="10" GridLines="Horizontal">
                    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
                        EmployeeName EmployeeID
                        <asp:Label id="label1" runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>'>
                        <%# DataBinder.Eval(Container.DataItem, "EmployeeID")%>
                <asp:label id="lblAllItems1" 
                    Text="The following items <br /> are copied to the array: " 
                    Runat="server" ForeColor="blue" Font-Bold="true" 
                    The following items <br /> are copied to the array:
                &nbsp;&nbsp;&nbsp; <b>Show Items:</b>
                <asp:RadioButtonList ID="listItemNo" 
                    OnSelectedIndexChanged="itemSelected" AutoPostBack="true" 
                    <asp:ListItem Value="0" Text="0"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                <asp:Label ID="lblDisplay" Runat="server" />
        <asp:button id="show" onclick="show_Click" Runat="server" 
             Font-Bold="True" Text="DataList Information" />
        <asp:button id="allitems" onclick="allItems_Click" Runat="server" 
            Font-Bold="True" Text="Show All DataListItems" />
        <b>All DataList items will be shown here:</b>
        <asp:label id="lblAllItems" Runat="server" ForeColor="blue" />

.NET Framework
Available since 1.1

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

Return to top