DetailsView.DataKeyNames 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定陣列,陣列中包含資料來源的索引鍵欄位名稱。
public:
virtual property cli::array <System::String ^> ^ DataKeyNames { cli::array <System::String ^> ^ get(); void set(cli::array <System::String ^> ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))]
public virtual string[] DataKeyNames { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))>]
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()
屬性值
- String[]
陣列,包含資料來源的索引鍵欄位名稱。
- 屬性
範例
下列程式碼範例示範如何使用 DataKeyNames 屬性來識別系結至 控制項之資料來源的 DetailsView 索引鍵欄位。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView AutoGenerateDeleteButton Example</h3>
<asp:detailsview id="CustomersDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneratedeletebutton="true"
autogeneraterows="true"
allowpaging="true"
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]">
</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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView AutoGenerateDeleteButton Example</h3>
<asp:detailsview id="CustomersDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneratedeletebutton="true"
autogeneraterows="true"
allowpaging="true"
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]">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
DataKeyNames使用 屬性可指定以逗號分隔的功能變數名稱清單,代表資料來源的主鍵。 DataKeyNames設定屬性時, DetailsView 控制項會自動建立 DataKey 物件,代表目前記錄的索引鍵欄位或欄位,並將它儲存在 屬性中 DataKey 。 AutoGenerateColumns當 屬性也設定為 true
時, DetailsView 控制項會自動確保屬性中指定的 DataKeyNames 欄位或欄位是唯讀的。
注意
您必須設定 DataKeyNames 控制項的 DetailsView 自動更新、刪除和插入功能的屬性才能運作。
Visible如果資料欄欄位的 屬性設定 false
為 ,則資料列不會顯示在 控制項中 DetailsView ,而且資料列的資料不會往返用戶端。 如果您想要不可見的資料列進行來回行程,請將功能變數名稱新增至 DataKeyNames 屬性。