DataKey 类

定义

表示数据绑定控件中某个记录的一个或多个主键字段。Represents the primary key field or fields of a record in a data-bound control.

public ref class DataKey : IEquatable<System::Web::UI::WebControls::DataKey ^>, System::Web::UI::IStateManager
public class DataKey : IEquatable<System.Web.UI.WebControls.DataKey>, System.Web.UI.IStateManager
type DataKey = class
    interface IStateManager
    interface IEquatable<DataKey>
Public Class DataKey
Implements IEquatable(Of DataKey), IStateManager
继承
DataKey
实现

示例

下面的代码示例演示如何使用DetailsView DataKey对象的Value属性来确定控件中记录的主键值。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="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类用于表示数据绑定控件中的记录的主键。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. DataKey尽管类不是集合, 但它可以存储多个键字段值。Although the DataKey class is not a collection, it can store multiple key field values. 调用DataKey类的构造函数之一时, 将填充键字段值。The key field values are populated when one of the constructors for the DataKey class is called. 可以通过以下方式从DataKey对象检索密钥字段值:You can retrieve a key field value from a DataKey object in the following ways:

  • 使用属性可检索DataKey对象中特定索引处的键字段值。 DataKey.Item[Int32]Use the DataKey.Item[Int32] property to retrieve a key field value at a specific index in the DataKey object.

  • DataKey.Item[String]使用属性可检索特定字段的键字段值。Use the DataKey.Item[String] property to retrieve a key field value of a specific field.

  • 使用属性可检索DataKey对象中索引0处的键字段的值。 ValueUse 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.

  • 使用属性创建可用于循环访问键字段值的对象。IOrderedDictionary ValuesUse the Values property to create an IOrderedDictionary object that can be used to iterate through the key field values.

通常, 当DataKey设置控件的DataKeyNames属性时, 数据绑定控件将自动生成对象。In general, DataKey objects are automatically generated by data-bound controls when the control's DataKeyNames property is set. 对象包含DataKeyNames属性中指定的一个或多个键字段的值。 DataKeyThe DataKey objects contain the values of the key field or fields specified in the DataKeyNames property. 每次显示一条记录的数据绑定控件 ( DetailsView如或FormView) 通常存储DataKey控件的DataKey属性中显示的当前记录的对象。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. 一次显示多个记录的数据绑定控件 (如GridView) 通常将控件中每个记录的DataKey对象存储在一个DataKeyArray集合中。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. 然后DataKeyArray , 集合存储DataKeys在控件的属性中。The DataKeyArray collection is then stored in the DataKeys property of the control.

构造函数

DataKey(IOrderedDictionary)

使用指定的键字段值字典初始化 DataKey 类的新实例。Initializes a new instance of the DataKey class using the specified dictionary of key field values.

DataKey(IOrderedDictionary, String[])

使用指定的键字段值字典和字段名数组初始化 DataKey 类的新实例。Initializes a new instance of the DataKey class using the specified dictionary of key field values and array of field names.

属性

IsTrackingViewState

获取一个值,该值指示 DataKey 对象是否跟踪其视图状态更改。Gets a value indicating whether the DataKey object is tracking its view-state changes.

Item[Int32]

DataKey 对象中的指定索引位置获取键字段的值。Gets the value of the key field at the specified index from a DataKey object.

Item[String]

DataKey 对象中获取具有指定字段名的键字段的值。Gets the value of the key field with the specified field name from a DataKey object.

Value

获取 DataKey 对象中索引 0 位置的键字段值。Gets the value of the key field at index 0 in the DataKey object.

Values

获取一个 IOrderedDictionary 对象,它包含 DataKey 对象中的每个键字段。Gets an IOrderedDictionary object that contains every key field in the DataKey object.

方法

Equals(DataKey)

确定指定的 DataKey 数组是否等于当前的数据键。Determines whether the specified DataKey array is equal to the current data key.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
LoadViewState(Object)

加载以前保存的 DataKey 对象的视图状态。Loads the previously saved view state of the DataKey object.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
SaveViewState()

保存 DataKey 对象的当前视图状态。Saves the current view state of the DataKey object.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
TrackViewState()

标记开始跟踪并将视图状态更改保存到 DataKey 对象的起点。Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.

显式界面实现

IStateManager.IsTrackingViewState

获取一个值,该值指示 DataKey 对象是否跟踪其视图状态更改。Gets a value indicating whether the DataKey object is tracking its view-state changes.

IStateManager.LoadViewState(Object)

加载以前保存的 DataKey 对象的视图状态。Loads the previously saved view state of the DataKey object.

IStateManager.SaveViewState()

保存 DataKey 对象的当前视图状态。Saves the current view state of the DataKey object.

IStateManager.TrackViewState()

标记开始跟踪并将视图状态更改保存到 DataKey 对象的起点。Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.

适用于

另请参阅