Export (0) Print
Expand All

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, IEnumerable
public final class DataListItemCollection implements ICollection, IEnumerable
public final class DataListItemCollection implements ICollection, IEnumerable
Not applicable.

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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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);
        myDataTable.Rows.Add(myDataRow);
    }
    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();
        myDataList.DataBind();
        // Create a Array to hold the DataSource.
        System.Array myArray = Array.CreateInstance(typeof(DataListItem),
            myDataList.Items.Count);
        // Copy the DataSource to an Array.
        myDataList.Items.CopyTo(myArray,0);
        PrintValues(myArray);
    }
}

// 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 />" + 
            ((Label)(currentItem.Controls[1])).Text;
    }
}

// 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();
    while(dataListEnumerator.MoveNext())
    {
        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 = 
        ((DataListItemCollection)(myDataListItemCollection.SyncRoot))[index];
    // Display the selected DataListItem onto a label.
    lblDisplay.Text = "<b>DataListItem" + index + " is : "
        + ((Label)(currentItem.Controls[1])).Text;
    currentItem = null;
    myDataListItemCollection = null;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>
        DataListItemCollection Example
    </title>
</head>
<body>
<form runat="server" id="Form1">
    <h3>
        DataListItemCollection Example
    </h3>
    <table>
        <tr>
            <td>
                <asp:datalist id="myDataList" runat="server" Font-Size="8pt" 
                    Font-Names="Verdana" BorderColor="black" CellSpacing="5"
                    CellPadding="10" GridLines="Horizontal">
                    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
                    <HeaderTemplate>
                        EmployeeName EmployeeID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label id="label1" runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>'>
                        </asp:Label>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <%# DataBinder.Eval(Container.DataItem, "EmployeeID")%>
                    </ItemTemplate>
                </asp:datalist>
            </td>
            <td>
                <asp:label id="lblAllItems1" 
                    Text="The following items <br /> are copied to the array: " 
                    Runat="server" ForeColor="blue" Font-Bold="true" 
                    AssociatedControlID="listItemNo">
                    The following items <br /> are copied to the array:
                </asp:label>
            </td>
            <td>
                &nbsp;&nbsp;&nbsp; <b>Show Items:</b>
                <asp:RadioButtonList ID="listItemNo" 
                    OnSelectedIndexChanged="itemSelected" AutoPostBack="true" 
                    Runat="server">
                    <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:RadioButtonList>
            </td>
            <td>
                <asp:Label ID="lblDisplay" Runat="server" />
            </td>
        </tr>
    </table>
    <p>
        <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" />
    </p>
    <p>
        <b>All DataList items will be shown here:</b>
        <asp:label id="lblAllItems" Runat="server" ForeColor="blue" />
    </p>
</form>
</body>
</html>

<%@ Page language="VJ#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.get_Columns().Add(new DataColumn("EmployeeName",
String.class.ToType()));
    myDataTable.get_Columns().Add(new DataColumn("EmployeeID",
long.class.ToType()));
    for(int i=0; i < 3; i++) {
myDataRow = myDataTable.NewRow();
myDataRow.set_Item(0,"somename" + System.Convert.ToString(i));
myDataRow.set_Item( 1 ,System.Convert.ToString(i + 1000 ));
myDataTable.get_Rows().Add(myDataRow);
    } 
    DataView dataView =  new DataView(myDataTable);
    return dataView;
} //CreateDataSource

void Page_Load(Object sender, EventArgs e) 
{
    if (!(get_IsPostBack())) {
    // Bind the DataView to the DataSource.
    myDataList.set_DataSource(CreateDataSource());
    myDataList.DataBind();

    // Create a Array to hold the DataSource.
    System.Array myArray = Array.CreateInstance(DataListItem .class.ToType(),
myDataList.get_Items().get_Count());

    // Copy the DataSource to an Array.
    myDataList.get_Items().CopyTo(myArray, 0);
    PrintValues(myArray);
    }
} //Page_Load

// 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.get_Current()));
lblAllItems1.set_Text(lblAllItems1.get_Text() + "<br /><br />" 
    +((Label)(currentItem.get_Controls() .get_Item( 1))).get_Text());
    }
} //PrintValues

// Event handler method for show button.
void Show_Click(Object sender, EventArgs e)
{
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.get_Items();
    
    // Display the read-only properties.
    get_Response().Write("<b>The Total number of items are " 
+ myDataListItemCollection.get_Count() + "</b>");
    get_Response().Write("<br /><b>The ReadOnly property of the"
+" DataListItemCollection is always " 
+ myDataListItemCollection.get_IsReadOnly() + "</b>");
    get_Response().Write("<br /><b>The IsSynchronized property of the"
+" DataListItemCollection is always " 
+ myDataListItemCollection.get_IsSynchronized() + "</b>");
    myDataListItemCollection = null;
} //Show_Click

void AllItems_Click(Object sender, EventArgs e)
{
    IEnumerator dataListEnumerator;
    DataListItem currentItem;
    lblAllItems.set_Text("");
    
    // Get an enumerator to traverse the DataListItemCollection.
    dataListEnumerator = myDataList.get_Items().GetEnumerator();
    while(dataListEnumerator.MoveNext()) {
currentItem =((DataListItem)(dataListEnumerator.get_Current()));
// Display the current DataListItem onto the label.
lblAllItems.set_Text(lblAllItems.get_Text() 
    +((Label)(currentItem.get_Controls() .get_Item( 1))).get_Text() 
    + " ");
    }
} //AllItems_Click

void ItemSelected(Object sender, EventArgs e)
{
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.get_Items();
    
    // Get the index of the selected radio button in the RadioButtonList.
    int index = Convert.ToInt16(listItemNo.get_SelectedItem().get_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 = ((DataListItemCollection)(
myDataListItemCollection.get_SyncRoot())).get_Item(index);

    // Display the selected DataListItem onto a label.
    lblDisplay.set_Text("<b>DataListItem" + index + " is : " 
+((Label)(currentItem.get_Controls() .get_Item( 1))).get_Text());
    currentItem = null;
    myDataListItemCollection = null;
} //ItemSelected
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>
            DataListItemCollection Example
        </title>
</head>
<body>
<form runat="server" id="Form1">
    <h3>
        DataListItemCollection Example
    </h3>
    <table>
        <tr>
            <td>
                <asp:datalist id="myDataList" runat="server" Font-Size="8pt" 
                    Font-Names="Verdana" BorderColor="black" CellSpacing="5"
                    CellPadding="10" GridLines="Horizontal">
                    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
                    <HeaderTemplate>
                        EmployeeName EmployeeID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label id="label1" 
                            Text='<%# DataBinder.Eval(Container.get_DataItem(), "EmployeeName") %>' runat="server" />
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <%# DataBinder.Eval(Container.get_DataItem(), "EmployeeID")%>
                    </ItemTemplate>
                </asp:datalist>
            </td>
            <td>
                <asp:label id="lblAllItems1" 
                    Text="The following items <br /> are copied to the array: " 
                    Runat="server" ForeColor="blue" Font-Bold="true" 
                    AssociatedControlID="listItemNo">
                    The following items <br /> are copied to the array:
                </asp:label>
            </td>
            <td>
                &nbsp;&nbsp;&nbsp; <b>Show Items:</b>
                <asp:RadioButtonList ID="listItemNo" 
                    OnSelectedIndexChanged="ItemSelected" AutoPostBack="true" 
                    Runat="server">
                    <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:RadioButtonList>
            </td>
            <td>
                <asp:Label ID="lblDisplay" Runat="server"></asp:Label>
            </td>
        </tr>
    </table>
    <p>
        <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" />
    </p>
    <p>
        <b>All DataList items will be shown here:</b>
        <asp:label id="lblAllItems" Runat="server" ForeColor="blue" />
    </p>
</form>
</body>
</html>

System.Object
  System.Web.UI.WebControls.DataListItemCollection

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

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft