DataKey 類別

定義

表示主索引鍵欄位或資料繫結控制項中記錄的欄位。Represents the primary key field or fields of a record in a data-bound control.

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
實作

範例

下列程式碼範例示範如何 DetailsView 使用物件的屬性,判斷控制項中記錄的主鍵值 Value DataKeyThe 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. 當呼叫類別的其中一個函式時,就會填入索引鍵域值 DataKeyThe key field values are populated when one of the constructors for the DataKey class is called. 您可以透過下列方式,從物件中取出索引鍵域值 DataKeyYou can retrieve a key field value from a DataKey object in the following ways:

  • DataKey.Item[Int32] 可以使用屬性,在物件中的特定索引處取得索引鍵域值 DataKeyUse 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.

  • Value 可以使用屬性,在物件的索引0取出索引鍵欄位的值 DataKeyUse 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.

  • Values 可以使用屬性來建立 IOrderedDictionary 可用於逐一查看索引鍵域值的物件。Use the Values property to create an IOrderedDictionary object that can be used to iterate through the key field values.

一般情況下, DataKey 物件會在設定控制項的屬性時,由資料繫結控制項自動產生 DataKeyNamesIn general, DataKey objects are automatically generated by data-bound controls when the control's DataKeyNames property is set. DataKey物件包含索引鍵欄位的值或屬性中指定的欄位 DataKeyNamesThe DataKey objects contain the values of the key field or fields specified in the DataKeyNames property. 一次顯示一筆記錄的資料繫結控制項 (例如 DetailsViewFormView) 通常會儲存 DataKey 控制項屬性中顯示的目前記錄物件 DataKeyData-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 每個記錄的物件儲存在集合中 DataKeyArrayData-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()

傳回代表目前物件的字串。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.

適用於

另請參閱