This documentation is archived and is not being maintained.

DataKey Class

Represents the primary key field or fields of a record in a data-bound control.

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

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class DataKey : IStateManager

The DataKey class is used to represent the primary key for a record in a data-bound control. The primary key for a record can be composed of one of more fields from the data source. Although the DataKey class is not a collection, it can store multiple key field values. The key field values are populated when one of the constructors for the DataKey class is called. You can retrieve a key field value from a DataKey object in the following ways:

  • Use the DataKey.Item[Int32] property to retrieve a key field value at a specific index in the DataKey object.

  • Use the DataKey.Item[String] property to retrieve a key field value of a specific field.

  • Use the Value property to retrieve the value of the key field at index 0 in the DataKey object. This property is often used as a shortcut to retrieve the key value of a record when the primary key contains only one field.

  • Use the Values property to create an IOrderedDictionary object that can be used to iterate through the key field values.

In general, DataKey objects are automatically generated by data-bound controls when the control's DataKeyNames property is set. The DataKey objects contain the values of the key field or fields specified in the DataKeyNames property. Data-bound controls that display a single record at a time (such as DetailsView or FormView) generally store the DataKey object for the current record displayed in the DataKey property of the control. Data-bound controls that display multiple records at a time (such as GridView) generally store the DataKey objects for each record in the control in a DataKeyArray collection. The DataKeyArray collection is then stored in the DataKeys property of the control.

The following code example demonstrates how to determine the primary key value of a record in a DetailsView control using the Value property of a DataKey object.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

  void CustomerDetailsView_DataBound(Object sender, EventArgs e)
    // Get the DataKey object for the current record.
    DataKey key = CustomerDetailsView.DataKey;

    // Display the the value of the key field.
    MessageLabel.Text = "The key field value for the displayed record is " + 
      key.Value.ToString() + ".";


<html xmlns="" >

  <head runat="server">
    <title>DataKey Example</title>
    <form id="form1" runat="server">

      <h3>DataKey Example</h3>

        <asp:detailsview id="CustomerDetailsView"



        <asp:label id="MessageLabel"

        <!-- 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="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 



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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0