Export (0) Print
Expand All

DataKeyArray Class

Note: This class is new in the .NET Framework version 2.0.

Represents a collection of DataKey objects. This class cannot be inherited.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public sealed class DataKeyArray : ICollection, IEnumerable, IStateManager
public final class DataKeyArray implements ICollection, IEnumerable, 
	IStateManager
public final class DataKeyArray implements ICollection, IEnumerable, 
	IStateManager

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="C#" %>

<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>

  <body>
    <form 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>


The following code example demonstrates how to iterate through a DataKeyArray collection.


<%@ Page language="C#" %>

<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>

  <body>
    <form 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>


System.Object
  System.Web.UI.WebControls.DataKeyArray

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft