Classe DataKeyArray
Aggiornamento: novembre 2007
Rappresenta un insieme di oggetti DataKey. Questa classe non può essere ereditata.
Assembly: System.Web (in System.Web.dll)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] public sealed class DataKeyArray : ICollection, IEnumerable, IStateManager
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */ public final class DataKeyArray implements ICollection, IEnumerable, IStateManager
public final class DataKeyArray implements ICollection, IEnumerable, IStateManager
La classe DataKeyArray è utilizzata per memorizzare e gestire un insieme di oggetti DataKey. Un oggetto DataKey rappresenta la chiave primaria di un record in un controllo con associazione a dati. In genere, i controlli con associazione a dati che visualizzano più record (ad esempio il controllo GridView) utilizzano un oggetto DataKeyArray per memorizzare gli oggetti DataKey per i record visualizzati nel controllo.
Mediante la classe DataKeyArray è possibile accedere in diversi modi agli elementi contenuti nell'insieme:
Utilizzare l'indicizzatore Item per recuperare direttamente un oggetto DataKey dall'insieme in corrispondenza di un indice in base zero specifico.
Utilizzare il metodo GetEnumerator per recuperare un enumeratore che può essere utilizzato per scorrere l'insieme.
Utilizzare il metodo CopyTo per copiare gli elementi contenuti nell'insieme in una matrice, che potrà quindi essere utilizzata per accedere a tali elementi.
Per determinare il numero totale di elementi contenuti nell'insieme, utilizzare la proprietà Count.
Nell'esempio di codice riportato di seguito viene illustrato come utilizzare l'indicizzatore per recuperare un oggetto DataKey da un insieme DataKeyArray.
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void CustomerGridView_DataBound(Object sender, EventArgs e) { // Use the indexer to retrieve the DataKey object for the // first record. DataKey key = CustomerGridView.DataKeys[0]; // Display the the value of the primary key for the first // record displayed in the GridView control. MessageLabel.Text = "The primary key of the first record displayed is " + key.Value.ToString() + "."; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>DataKeyArray Example</title> </head> <body> <form id="form1" runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" ondatabound="CustomerGridView_DataBound" runat="server"> </asp:gridview> <br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
Nell'esempio di codice riportato di seguito viene illustrato come scorrere un insieme DataKeyArray.
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void CustomerGridView_DataBound(Object sender, EventArgs e) { // Display the the value of the primary key for each // record in the GridView control. MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"; foreach (DataKey key in CustomerGridView.DataKeys) { MessageLabel.Text += key.Value.ToString() + "<br/>"; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>DataKeyArray Example</title> </head> <body> <form id="form1" runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" ondatabound="CustomerGridView_DataBound" runat="server"> </asp:gridview> <br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
- AspNetHostingPermission
per operare in un ambiente host. Valore richiesta: LinkDemand. Valore autorizzazione: Minimal.
- AspNetHostingPermission
per operare in un ambiente host. Valore richiesta: InheritanceDemand. Valore autorizzazione: Minimal.
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.