DetailsView.DataKey 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 DataKey 物件,表示所顯示資料錄的主索引鍵。
public:
virtual property System::Web::UI::WebControls::DataKey ^ DataKey { System::Web::UI::WebControls::DataKey ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataKey DataKey { get; }
[<System.ComponentModel.Browsable(false)>]
member this.DataKey : System.Web.UI.WebControls.DataKey
Public Overridable ReadOnly Property DataKey As DataKey
屬性值
DataKey,表示所顯示資料錄的主索引鍵。
- 屬性
範例
下列程式碼範例示範如何使用 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 CustomerDetailView_ItemUpdated(Object sender,
DetailsViewUpdatedEventArgs e)
{
// Create a dictionary that contains the key fields and values using
// the AllValues method of the DataKey object contained in the DataKey
// property.
IOrderedDictionary keyList = CustomerDetailView.DataKey.Values;
// Get the ArrayList objects that represent the key fields and values.
ArrayList keys = (ArrayList)keyList.Keys;
ArrayList values = (ArrayList)keyList.Values;
// Get the key field and value for the current record.
String keyField = keys[0].ToString();
String keyValue = values[0].ToString();
// Log the update operation using the key field and value.
LogUpdate(keyField, keyValue);
}
void LogUpdate(String keyField, String keyValue)
{
// Insert code to log the update operation.
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView DataKey Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView DataKey Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
autogenerateeditbutton="true"
allowpaging="true"
onitemupdated="CustomerDetailView_ItemUpdated"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<!-- 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" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From [Customers]"
UpdateCommand="UPDATE [Customers] SET
[CompanyName] = @CompanyName,
[Address] = @Address, [City] = @City,
[PostalCode] = @PostalCode, [Country] = @Country
WHERE [CustomerID] = @CustomerID">
</asp:SqlDataSource>
</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 CustomerDetailView_ItemUpdated(ByVal sender As Object, _
ByVal e As DetailsViewUpdatedEventArgs)
' Create a dictionary that contains the key fields and values
' using the AllValues method of the DataKey object contained
' in the DataKey property.
Dim keyList As IOrderedDictionary = _
CustomerDetailsView.DataKey.Values
' Get the ArrayList objects that represent the key fields
' and values.
Dim keys As ArrayList = CType(keyList.Keys, ArrayList)
Dim values As ArrayList = CType(keyList.Values, ArrayList)
' Get the key field and value for the current record.
Dim keyField As String = keys(0).ToString()
Dim keyValue As String = values(0).ToString()
' Log the update operation using the key field and value.
LogUpdate(keyField, keyValue)
End Sub
Sub LogUpdate(ByVal keyField As String, ByVal keyValue As String)
' Insert code to log the update operation.
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView DataKey Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView DataKey Example</h3>
<asp:DetailsView ID="CustomerDetailsView"
DataSourceID="DetailsViewSource"
DataKeyNames="CustomerID"
AutoGenerateRows="true"
AutoGenerateEditButton="true"
AllowPaging="true"
OnItemUpdated="CustomerDetailView_ItemUpdated"
runat="server">
<HeaderStyle BackColor="Navy" ForeColor="White" />
</asp:DetailsView>
<!-- 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" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country]
From [Customers]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,
[Address] = @Address, [City] = @City,
[PostalCode] = @PostalCode, [Country] = @Country
WHERE [CustomerID] = @CustomerID"></asp:SqlDataSource>
</form>
</body>
</html>
備註
DataKeyNames設定 屬性時, DetailsView 控制項會自動建立 DataKey 物件,代表目前記錄的索引鍵欄位或欄位。 DataKey使用 屬性來取得這個 DataKey 物件。 這個屬性通常用來取得屬性中所 DataKeyNames 指定欄位的索引鍵及其對應值。
注意
做為快捷方式,您也可以使用 SelectedValue 屬性來判斷 屬性中列出的 DataKeyNames 第一個索引鍵欄位的值。