DataKeyArray Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Représente une collection d’objets DataKey . Cette classe ne peut pas être héritée.
public ref class DataKeyArray sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class DataKeyArray : System.Collections.ICollection, System.Web.UI.IStateManager
type DataKeyArray = class
interface ICollection
interface IEnumerable
interface IStateManager
Public NotInheritable Class DataKeyArray
Implements ICollection, IStateManager
- Héritage
-
DataKeyArray
- Implémente
Exemples
L’exemple de code suivant montre comment utiliser l’indexeur pour récupérer un DataKey objet à partir d’une DataKeyArray collection.
<%@ 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 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() + ".";
}
void CopyArray_Click(Object sender, EventArgs e)
{
DataKeyArray theKeys = CustomerGridView.DataKeys;
DataKey[] myNewArray = new DataKey[theKeys.Count];
theKeys.CopyTo(myNewArray, 0);
Label2.Visible = true;
// Display first page key values from the new array.
for (int i = 0; i < myNewArray.Length; i++)
{
Label2.Text += "<br />" + myNewArray[i].Value;
}
}
</script>
<html >
<head id="Head1" 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"/>
<asp:Button ID="CopyArray"
runat="server"
Text="Copy DataKeyArray to Array"
OnClick="CopyArray_Click" />
<br />
<asp:label id="Label2"
runat="server"
Visible="false"
Text="First page of Copied Array Key Values" />
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
' Use the indexer to retrieve the DataKey object for the
' first record.
Dim key As DataKey = CustomerGridView.DataKeys(0)
' Display 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() & "."
End Sub
Sub CopyArray_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim theKeys As DataKeyArray = CustomerGridView.DataKeys
Dim myNewArray(theKeys.Count - 1) As DataKey
theKeys.CopyTo(myNewArray, 0)
Label2.Visible = True
' Display first page key values from the new array.
For i As Integer = 0 To myNewArray.Length - 1
Label2.Text &= "<br />" & myNewArray(i).Value
Next i
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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"
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"/>
<asp:Button ID="CopyArray"
runat="server"
Text="Copy DataKeyArray to Array"
OnClick="CopyArray_Click" />
<br />
<asp:label id="Label2"
runat="server"
Visible="false"
Text="First page of Copied Array Key Values" />
</form>
</body>
</html>
L’exemple de code suivant montre comment effectuer une itération dans une DataKeyArray collection.
<%@ 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 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>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
' Display 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/>"
Dim key As DataKey
For Each key In CustomerGridView.DataKeys
MessageLabel.Text += key.Value.ToString() + "<br/>"
Next
End Sub
</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"
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>
Remarques
La DataKeyArray classe est utilisée pour stocker et gérer une collection d’objets DataKey . Un DataKey objet représente la clé primaire d’un enregistrement dans un contrôle lié aux données. En général, les contrôles liés aux données qui affichent plusieurs enregistrements (tels que le GridView contrôle) utilisent un DataKeyArray objet pour stocker les DataKey objets des enregistrements affichés dans le contrôle.
La DataKeyArray classe prend en charge plusieurs façons d’accéder aux éléments de la collection :
Utilisez l’indexeur Item[] pour récupérer directement un DataKey objet de la collection à un index de base zéro spécifique.
Utilisez la GetEnumerator méthode pour récupérer un énumérateur qui peut être utilisé pour itérer dans la collection.
Utilisez la CopyTo méthode pour copier les éléments de la collection dans un tableau, qui peut ensuite être utilisé pour accéder aux éléments de la collection.
Pour déterminer le nombre total d’éléments de la collection, utilisez la Count propriété.
Constructeurs
| Nom | Description |
|---|---|
| DataKeyArray(ArrayList) |
Initialise une nouvelle instance de la classe DataKeyArray. |
Propriétés
| Nom | Description |
|---|---|
| Count |
Obtient le nombre d'éléments dans la collection. |
| IsReadOnly |
Obtient une valeur indiquant si les éléments de la collection peuvent être modifiés. |
| IsSynchronized |
Obtient une valeur indiquant si la DataKeyArray collection est synchronisée (thread safe). |
| Item[Int32] |
Obtient l’objet DataKey de la collection à l’index spécifié. |
| SyncRoot |
Obtient l’objet utilisé pour synchroniser l’accès à la collection. |
Méthodes
| Nom | Description |
|---|---|
| CopyTo(DataKey[], Int32) |
Copie tous les éléments de cette collection dans le tableau d’objets DataKey spécifié, en commençant à l’index spécifié dans le tableau. |
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetEnumerator() |
Retourne un énumérateur qui contient tous les DataKey objets de la collection. |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |
Implémentations d’interfaces explicites
| Nom | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Copie tous les éléments de cette collection vers l’index spécifié Array, en commençant à l’index spécifié dans le Array. |
| IStateManager.IsTrackingViewState |
Obtient une valeur indiquant si l’objet DataKeyArray suit ses modifications d’état d’affichage. |
| IStateManager.LoadViewState(Object) |
Charge l’état d’affichage précédemment enregistré de l’objet DataKeyArray . |
| IStateManager.SaveViewState() |
Enregistre l’état d’affichage actuel de l’objet DataKeyArray . |
| IStateManager.TrackViewState() |
Marque le point de départ auquel commencer le suivi et l’enregistrement des modifications de l’état d’affichage de l’objet DataKeyArray . |
Méthodes d’extension
| Nom | Description |
|---|---|
| AsParallel(IEnumerable) |
Active la parallélisation d’une requête. |
| AsQueryable(IEnumerable) |
Convertit un IEnumerable en IQueryable. |
| Cast<TResult>(IEnumerable) |
Convertit les éléments d’un IEnumerable en type spécifié. |
| OfType<TResult>(IEnumerable) |
Filtre les éléments d’une IEnumerable en fonction d’un type spécifié. |