DataListItemCollection Class
Assembly: System.Web (in system.web.dll)
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.
<%@ 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" />
<%# 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>
<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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.