Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:
.NET Framework Class Library

DataKey Class

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

System.Object
  System.Web.UI.WebControls.DataKey

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Public Class DataKey _
	Implements IStateManager, IEquatable(Of DataKey)

The DataKey type exposes the following members.

  Name Description
Public method DataKey(IOrderedDictionary) Initializes a new instance of the DataKey class using the specified dictionary of key field values.
Public method DataKey(IOrderedDictionary, String[]) Initializes a new instance of the DataKey class using the specified dictionary of key field values and array of field names.
Top
  Name Description
Protected property IsTrackingViewState Gets a value indicating whether the DataKey object is tracking its view-state changes.
Public property Item[Int32] Gets the value of the key field at the specified index from a DataKey object.
Public property Item[String] Gets the value of the key field with the specified field name from a DataKey object.
Public property Value Gets the value of the key field at index 0 in the DataKey object.
Public property Values Gets an IOrderedDictionary object that contains every key field in the DataKey object.
Top
  Name Description
Public method Equals(DataKey) Determines whether the specified DataKey array is equal to the current data key.
Public method Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method LoadViewState Loads the previously saved view state of the DataKey object.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method SaveViewState Saves the current view state of the DataKey object.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Protected method TrackViewState Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.
Top
  Name Description
Explicit interface implemetation Private property IStateManager.IsTrackingViewState Gets a value indicating whether the DataKey object is tracking its view-state changes.
Explicit interface implemetation Private method IStateManager.LoadViewState Infrastructure. Loads the previously saved view state of the DataKey object.
Explicit interface implemetation Private method IStateManager.SaveViewState Infrastructure. Saves the current view state of the DataKey object.
Explicit interface implemetation Private method IStateManager.TrackViewState Infrastructure. Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.
Top

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="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 CustomerDetailsView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerDetailsView.DataBound

    ' Get the DataKey object for the current record.
    Dim key As DataKey = CustomerDetailsView.DataKey

    ' Display the the value of the key field.
    MessageLabel.Text = "The key field value for the displayed record is " & _
      key.Value.ToString() & "."

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

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

      <h3>DataKey Example</h3>

        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          datakeynames="CustomerID"  
          allowpaging="true"
          runat="server">

        </asp:detailsview>

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

      </form>
  </body>
</html>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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