DataListItemCollection Class
Collapse the table of content
Expand the table of content

DataListItemCollection Class

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, 

The DataListItemCollection type exposes the following members.

Public methodDataListItemCollectionInitializes a new instance of the DataListItemCollection class.

Public propertyCountGets the number of DataListItem objects in the collection.
Public propertyIsReadOnlyGets a value that indicates whether the DataListItem objects in the DataListItemCollection can be modified.
Public propertyIsSynchronizedGets a value indicating whether access to the DataListItemCollection is synchronized (thread-safe).
Public propertyItemGets a DataListItem object at the specified index in the collection.
Public propertySyncRootGets the object that can be used to synchronize access to the DataListItemCollection collection.

Public methodCopyToCopies all the items from this DataListItemCollection collection to the specified System.Array object, starting at the specified index in the System.Array object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns a System.Collections.IEnumerator interface that contains all DataListItem objects in the DataListItemCollection.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public Extension MethodAsParallel()Overloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsParallel()Overloaded. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryable()Overloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodAsQueryable()Overloaded. (Defined by Queryable.)
Public Extension MethodCast<TResult>()Overloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodCast<TResult>()Overloaded. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>()Overloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>()Overloaded. (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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 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.
© 2015 Microsoft