DataKeyArray Class
Assembly: System.Web (in system.web.dll)
'Declaration Public NotInheritable Class DataKeyArray Implements ICollection, IEnumerable, IStateManager 'Usage Dim instance As DataKeyArray
public final class DataKeyArray implements ICollection, IEnumerable, IStateManager
public final class DataKeyArray implements ICollection, IEnumerable, IStateManager
Not applicable.
The DataKeyArray class is used to store and manage a collection of DataKey objects. A DataKey object represents the primary key of a record in a data-bound control. In general, data-bound controls that display multiple records (such as the GridView control) use a DataKeyArray object to store the DataKey objects for the records displayed in the control.
The DataKeyArray class supports several ways to access the items in the collection:
-
Use the Item indexer to directly retrieve a DataKey object from the collection at a specific zero-based index.
-
Use the GetEnumerator method to retrieve an enumerator that can be used to iterate through the collection.
-
Use the CopyTo method to copy the items in the collection into an array, which can then be used to access the items in the collection.
To determine the total number of items in the collection, use the Count property.
The following code example demonstrates how to use the indexer to retrieve a DataKey object from a DataKeyArray collection.
<%@ 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 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 </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>
The following code example demonstrates how to iterate through a DataKeyArray collection.
<%@ 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 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.