DataKey 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示数据绑定控件中某个记录的一个或多个主键字段。
public ref class DataKey : System::Web::UI::IStateManager
	public ref class DataKey : IEquatable<System::Web::UI::WebControls::DataKey ^>, System::Web::UI::IStateManager
	public class DataKey : System.Web.UI.IStateManager
	public class DataKey : IEquatable<System.Web.UI.WebControls.DataKey>, System.Web.UI.IStateManager
	type DataKey = class
    interface IStateManager
	type DataKey = class
    interface IStateManager
    interface IEquatable<DataKey>
	Public Class DataKey
Implements IStateManager
	Public Class DataKey
Implements IEquatable(Of DataKey), IStateManager
		- 继承
 - 
				DataKey
 
- 实现
 
示例
下面的代码示例演示如何使用 Value 对象的 属性确定控件中DetailsView记录的主DataKey键值。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  void CustomerDetailsView_DataBound(Object sender, EventArgs e)
  {
    // Get the DataKey object for the current record.
    DataKey key = CustomerDetailsView.DataKey;
    
    // Display the value of the key field.
    MessageLabel.Text = "The key field value for the displayed record is " + 
      key.Value.ToString() + ".";
  }
  
</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"
          ondatabound="CustomerDetailsView_DataBound" 
          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>
<%@ 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 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>
	注解
类 DataKey 用于表示数据绑定控件中记录的主键。 记录的主键可以由数据源中的多个字段之一组成。 DataKey虽然 类不是集合,但它可以存储多个键字段值。 调用 类的构造函数 DataKey 之一时,将填充键字段值。 可通过以下方式从 DataKey 对象检索键字段值:
DataKey.Item[Int32]使用 属性检索 对象中特定索引处的DataKey键字段值。
DataKey.Item[String]使用 属性检索特定字段的键字段值。
Value使用 属性检索 对象中索引 0 处DataKey的键字段的值。 当主键仅包含一个字段时,此属性通常用作检索记录的键值的快捷方式。
Values使用 属性创建IOrderedDictionary可用于循环访问键字段值的对象。
通常, DataKey 设置控件的 属性时 DataKeyNames ,对象由数据绑定控件自动生成。 对象 DataKey 包含属性中指定的 DataKeyNames 一个或多个键字段的值。 一次显示单个记录的数据绑定控件 ((如 DetailsView 或 FormView) )通常存储 DataKey 控件的 属性中显示的 DataKey 当前记录的对象。 一次显示多个记录的数据绑定控件 (例如 GridView) 通常会将控件中每个记录的对象存储在 DataKey 集合 DataKeyArray 中。 然后,该 DataKeyArray 集合将存储在 控件的 属性中 DataKeys 。
构造函数
| DataKey(IOrderedDictionary) | 
		 使用指定的键字段值字典初始化 DataKey 类的新实例。  | 
        	
| DataKey(IOrderedDictionary, String[]) | 
		 使用指定的键字段值字典和字段名数组初始化 DataKey 类的新实例。  | 
        	
属性
| IsTrackingViewState | 
		 获取一个值,该值指示 DataKey 对象是否跟踪其视图状态更改。  | 
        	
| Item[Int32] | 
		 从 DataKey 对象中的指定索引位置获取键字段的值。  | 
        	
| Item[String] | 
		 从 DataKey 对象中获取具有指定字段名的键字段的值。  | 
        	
| Value | 
		 获取 DataKey 对象中索引 0 位置的键字段值。  | 
        	
| Values | 
		 获取一个 IOrderedDictionary 对象,它包含 DataKey 对象中的每个键字段。  | 
        	
方法
| Equals(DataKey) | 
		 确定指定的 DataKey 数组是否等于当前的数据键。  | 
        	
| Equals(Object) | 
		 确定指定对象是否等于当前对象。 (继承自 Object) | 
        	
| GetHashCode() | 
		 作为默认哈希函数。 (继承自 Object) | 
        	
| GetType() | 
		 获取当前实例的 Type。 (继承自 Object) | 
        	
| LoadViewState(Object) | 
		 加载以前保存的 DataKey 对象的视图状态。  | 
        	
| MemberwiseClone() | 
		 创建当前 Object 的浅表副本。 (继承自 Object) | 
        	
| SaveViewState() | 
		 保存 DataKey 对象的当前视图状态。  | 
        	
| ToString() | 
		 返回表示当前对象的字符串。 (继承自 Object) | 
        	
| TrackViewState() | 
		 标记开始跟踪并将视图状态更改保存到 DataKey 对象的起点。  | 
        	
显式接口实现
| IStateManager.IsTrackingViewState | 
		 获取一个值,该值指示 DataKey 对象是否跟踪其视图状态更改。  | 
        	
| IStateManager.LoadViewState(Object) | 
		 加载以前保存的 DataKey 对象的视图状态。  | 
        	
| IStateManager.SaveViewState() | 
		 保存 DataKey 对象的当前视图状态。  | 
        	
| IStateManager.TrackViewState() | 
		 标记开始跟踪并将视图状态更改保存到 DataKey 对象的起点。  |