DataKey DataKey DataKey DataKey Class

定義

表示資料繫結控制項中資料錄的主索引鍵欄位。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
繼承
DataKeyDataKeyDataKeyDataKey
實作

範例

下列程式碼範例示範如何判斷中記錄的主索引鍵值DetailsView使用控制Value屬性DataKey物件。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.Item[Int32]屬性,以擷取中的特定索引處的索引鍵欄位值DataKey物件。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.

  • 使用Value屬性擷取中的索引 0 處的索引鍵欄位值DataKey物件。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.

  • 使用Values屬性,以建立IOrderedDictionary可用來逐一查看索引鍵欄位值的物件。Use 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. DataKey物件包含索引鍵欄位或欄位中指定的值DataKeyNames屬性。The 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(IOrderedDictionary) DataKey(IOrderedDictionary) DataKey(IOrderedDictionary)

使用指定之索引鍵欄位值的字典初始化 DataKey 類別的新執行個體。Initializes a new instance of the DataKey class using the specified dictionary of key field values.

DataKey(IOrderedDictionary, String[]) DataKey(IOrderedDictionary, String[]) DataKey(IOrderedDictionary, String[]) 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 IsTrackingViewState IsTrackingViewState IsTrackingViewState

取得值,指出 DataKey 物件是否正在追蹤它的檢視狀態變更。Gets a value indicating whether the DataKey object is tracking its view-state changes.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

取得 DataKey 物件中之指定索引處的索引鍵欄位值。Gets the value of the key field at the specified index from a DataKey object.

Item[String] Item[String] Item[String] Item[String]

取得 DataKey 物件中具有指定之欄位名稱的索引鍵欄位值。Gets the value of the key field with the specified field name from a DataKey object.

Value Value Value Value

取得 DataKey 物件中之索引 0 處的索引鍵欄位值。Gets the value of the key field at index 0 in the DataKey object.

Values Values Values Values

取得 IOrderedDictionary 物件,包含 DataKey 物件中的每個索引鍵欄位。Gets an IOrderedDictionary object that contains every key field in the DataKey object.

方法

Equals(DataKey) Equals(DataKey) Equals(DataKey) Equals(DataKey)

判斷指定的 DataKey 陣列是否等於目前的資料索引鍵。Determines whether the specified DataKey array is equal to the current data key.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
LoadViewState(Object) LoadViewState(Object) LoadViewState(Object) LoadViewState(Object)

載入先前儲存的 DataKey 物件檢視狀態。Loads the previously saved view state of the DataKey object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
SaveViewState() SaveViewState() SaveViewState() SaveViewState()

儲存 DataKey 物件的目前檢視狀態。Saves the current view state of the DataKey object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
TrackViewState() TrackViewState() TrackViewState() TrackViewState()

標示起點,在開始追蹤並儲存檢視狀態變更至 DataKey 物件。Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.

明確介面實作

IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState

取得值,指出 DataKey 物件是否正在追蹤它的檢視狀態變更。Gets a value indicating whether the DataKey object is tracking its view-state changes.

IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object)

載入先前儲存的 DataKey 物件檢視狀態。Loads the previously saved view state of the DataKey object.

IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState()

儲存 DataKey 物件的目前檢視狀態。Saves the current view state of the DataKey object.

IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState()

標示起點,在開始追蹤並儲存檢視狀態變更至 DataKey 物件。Marks the starting point at which to begin tracking and saving view-state changes to the DataKey object.

適用於

另請參閱