DetailsView.DataKey Property


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 Overridable ReadOnly Property DataKey As DataKey

Property Value

Type: System.Web.UI.WebControls.DataKey

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="VB" %>

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

    Sub CustomerDetailView_ItemUpdated(ByVal sender As Object, _
        ByVal e As DetailsViewUpdatedEventArgs)

        ' Create a dictionary that contains the key fields and values 
        ' using the AllValues method of the DataKey object contained 
        ' in the DataKey property.

        Dim keyList As IOrderedDictionary = _

        ' Get the ArrayList objects that represent the key fields 
        ' and values.
        Dim keys As ArrayList = CType(keyList.Keys, ArrayList)
        Dim values As ArrayList = CType(keyList.Values, ArrayList)

        ' Get the key field and value for the current record. 
        Dim keyField As String = keys(0).ToString()
        Dim keyValue As String = values(0).ToString()

        ' Log the update operation using the key field and value.
        LogUpdate(keyField, keyValue)

    End Sub

    Sub LogUpdate(ByVal keyField As String, ByVal keyValue As String)

        ' Insert code to log the update operation.

    End Sub


<html xmlns="" >
<head runat="server">
            DetailsView DataKey Example</title>
    <form id="Form1" runat="server">
            DetailsView DataKey Example</h3>
        <asp:DetailsView ID="CustomerDetailsView" 

          <HeaderStyle BackColor="Navy" ForeColor="White" />

        <!-- 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"></asp:SqlDataSource>

.NET Framework
Available since 2.0
Return to top