DataKey 類別

定義

表示主索引鍵欄位或資料繫結控制項中記錄的欄位。

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

範例

下列程式碼範例示範如何使用 物件的 屬性,判斷 控制項 ValueDetailsView 記錄的主 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使用 屬性來擷取 物件中 DataKey 索引 0 索引鍵欄位的值。 當主鍵只包含一個欄位時,這個屬性通常用來擷取記錄的索引鍵值。

  • Values使用 屬性可建立可用來逐一 IOrderedDictionary 查看索引鍵域值的物件。

一般而言,設定控制項 DataKeyNames 的 屬性時, DataKey 資料繫結控制項會自動產生物件。 DataKey物件包含 屬性中指定的 DataKeyNames 索引鍵欄位或欄位的值。 一次顯示單一記錄的資料繫結控制項 (例如 DetailsViewFormView) 通常會儲存 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 物件。

適用於

另請參閱