DetailsView.DataKey Property

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

Gets a DataKey object that represents the primary key of the displayed record.

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

public virtual DataKey DataKey { get; }
/** @property */
public DataKey get_DataKey ()

public function get DataKey () : DataKey

Property Value

A DataKey that represents the primary key of the displayed record.

When the DataKeyNames property is set, the DetailsView control automatically creates a DataKey object that represents the key field or fields for the current record. Use the DataKey property to get this DataKey object. This property is commonly used to obtain the keys and their corresponding values for the fields specified in the DataKeyNames property.


As a shortcut, you can also use the SelectedValue property to determine the value of the first key field listed in the DataKeyNames property.

The following code example demonstrates how to use the DataKey property to determine the key field and corresponding value for the current record.

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

<script runat="server">

  void CustomerDetailView_ItemUpdated(Object sender, 
    DetailsViewUpdatedEventArgs e)
    // Create a dictionary that contains the key fields and values using
    // the AllValues method of the DataKey object contained in the DataKey 
    // property.
    IOrderedDictionary keyList = CustomerDetailView.DataKey.Values;

    // Get the ArrayList objects that represent the key fields and values.
    ArrayList keys = (ArrayList)keyList.Keys;
    ArrayList values = (ArrayList)keyList.Values;

    // Get the key field and value for the current record. 
    String keyField = keys[0].ToString();
    String keyValue = values[0].ToString();
    // Log the update operation using the key field and value.
    LogUpdate(keyField, keyValue);


  void LogUpdate(String keyField, String keyValue)
    // Insert code to log the update operation.


    <form id="Form1" runat="server">
      <h3>DetailsView DataKey Example</h3>
        <asp:detailsview id="CustomerDetailView"
          <headerstyle backcolor="Navy"
        <!-- 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" runat="server" 
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
                    InsertCommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] From [Customers]"
          UpdateCommand="UPDATE [Customers] SET 
            [CompanyName] = @CompanyName, 
            [Address] = @Address, [City] = @City,
            [PostalCode] = @PostalCode, [Country] = @Country
            WHERE [CustomerID] = @CustomerID">

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