Cet article a fait l'objet d'une traduction manuelle. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. |
Traduction
Source
|
DataListItemCollection, classe
Représente la collection d'objets DataListItem dans le contrôle DataList. Cette classe ne peut pas être héritée.
Assembly : System.Web (dans System.Web.dll)
Le type DataListItemCollection expose les membres suivants.
| Nom | Description | |
|---|---|---|
|
DataListItemCollection | Initialise une nouvelle instance de la classe DataListItemCollection. |
| Nom | Description | |
|---|---|---|
|
Count | Obtient le nombre d'objets DataListItem de la collection. |
|
IsReadOnly | Obtient une valeur qui indique si les objets DataListItem de DataListItemCollection peuvent être modifiés. |
|
IsSynchronized | Obtient une valeur indiquant si l'accès à DataListItemCollection est synchronisé (thread-safe). |
|
Item | Obtient un objet DataListItem situé à l'index spécifié dans la collection. |
|
SyncRoot | Obtient l'objet qui peut être utilisé pour synchroniser l'accès à la collection DataListItemCollection. |
| Nom | Description | |
|---|---|---|
|
CopyTo | Copie tous les éléments de cette collection DataListItemCollection dans l'objet System.Array spécifié, en commençant à l'index spécifié dans l'objet System.Array. |
|
Equals(Object) | Détermine si l'Object spécifié est égal à l'Object en cours. (Hérité de Object.) |
|
Finalize | Autorise un objet à tenter de libérer des ressources et d'exécuter d'autres opérations de netto***ge avant qu'il ne soit récupéré par l'opération garbage collection. (Hérité de Object.) |
|
GetEnumerator | Retourne une interface System.Collections.IEnumerator qui contient tous les objets DataListItem de DataListItemCollection. |
|
GetHashCode | Sert de fonction de hachage pour un type particulier. (Hérité de Object.) |
|
GetType | Obtient le Type de l'instance actuelle. (Hérité de Object.) |
|
MemberwiseClone | Crée une copie superficielle de l'objet Object actif. (Hérité de Object.) |
|
ToString | Retourne une chaîne qui représente l'objet actuel. (Hérité de Object.) |
| Nom | Description | |
|---|---|---|
|
AsParallel | Active la parallélisation d'une requête. (Défini par ParallelEnumerable.) |
|
AsQueryable | Convertit un IEnumerable en IQueryable. (Défini par Queryable.) |
|
Cast<TResult> | Convertit les éléments d'un IEnumerable vers le type spécifié. (Défini par Enumerable.) |
|
OfType<TResult> | Filtre les éléments d'un IEnumerable en fonction du type spécifié. (Défini par Enumerable.) |
La classe DataListItemCollection représente une collection d'objets DataListItem, qui représentent à leur tour les éléments de données dans un contrôle DataList. Pour récupérer par programme des objets DataListItem d'un contrôle DataList, utilisez l'une des méthodes suivantes :
-
Utilisez l'indexeur pour obtenir un seul objet DataListItem de la collection, à l'aide d'une notation de tableau.
-
Utilisez la méthode CopyTo pour copier le contenu de la collection vers un objet System.Array, qui peut ensuite être utilisé pour obtenir des éléments de la collection.
-
Utilisez la méthode GetEnumerator pour créer une interface System.Collections.IEnumerator, pouvant ensuite être utilisée pour obtenir des éléments de la collection.
La propriété Count spécifie le nombre total d'éléments dans la collection et est généralement utilisée pour déterminer la limite supérieure de la collection.
L'exemple suivant crée une source de données sous la forme d'un contrôle DataView qui est lié à un contrôle DataList déclaré dans la balise. Il affiche ensuite le contenu de l'objet DataListItemCollection sous-jacent dans la 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> <%# 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> <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>
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.