DataKey Class
Assembly: System.Web (in system.web.dll)
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.
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.