GridView.DataKeyNames 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定陣列,這個陣列包含 GridView 控制項中所顯示項目的主索引鍵欄位名稱。
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()
屬性值
陣列,包含 GridView 控制項中所顯示項目的主索引鍵欄位名稱。
- 屬性
範例
下列範例示範如何使用 DataKeyNames 屬性來指定資料來源的索引鍵欄位。 在此範例中, DataKeyNames
標記中專案的 GridView
屬性會使用逗號分隔名稱來指定兩個索引鍵欄位。 若要執行此範例,請建立具有下列專案的網站:
AdventureWorksLT 範例資料庫的連線,以及名為 的
AdventureWorksLTConnectionString
連接字串。 如需如何設定 AdventureWorksLT 範例資料庫的資訊,請參閱 如何:設定 AdventureWorksLT 範例資料庫以進行 ASP.NET 開發。名為
AdventureWorksLTDataClassesDataContext
的 LINQ-to-SQL 資料內容類別別。 資料內容必須具有 SalesOrderDetails 資料表的類別。 如需如何建立 LINQ-to-SQL 類別的資訊,請參閱LINQ to SQL。
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AdventureWorksLTDataClassesDataContext"
EnableDelete="True" EnableInsert="True" EnableUpdate="True"
TableName="SalesOrderDetails">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesOrderID,SalesOrderDetailID"
DataSourceID="LinqDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:BoundField DataField="SalesOrderID"
HeaderText="SalesOrderID" ReadOnly="True"
SortExpression="SalesOrderID" />
<asp:BoundField DataField="SalesOrderDetailID"
HeaderText="SalesOrderDetailID" InsertVisible="False"
ReadOnly="True" SortExpression="SalesOrderDetailID" />
<asp:BoundField DataField="OrderQty"
HeaderText="OrderQty" SortExpression="OrderQty" />
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" SortExpression="ProductID" />
<asp:BoundField DataField="UnitPrice"
HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="ModifiedDate"
HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AdventureWorksLTDataClassesDataContext"
EnableDelete="True" EnableInsert="True" EnableUpdate="True"
TableName="SalesOrderDetails">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesOrderID,SalesOrderDetailID"
DataSourceID="LinqDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:BoundField DataField="SalesOrderID"
HeaderText="SalesOrderID" ReadOnly="True"
SortExpression="SalesOrderID" />
<asp:BoundField DataField="SalesOrderDetailID"
HeaderText="SalesOrderDetailID" InsertVisible="False"
ReadOnly="True" SortExpression="SalesOrderDetailID" />
<asp:BoundField DataField="OrderQty"
HeaderText="OrderQty" SortExpression="OrderQty" />
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" SortExpression="ProductID" />
<asp:BoundField DataField="UnitPrice"
HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="ModifiedDate"
HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
</Columns>
</asp:GridView>
備註
DataKeyNames使用 屬性來指定代表資料來源主鍵的欄位或欄位。 您應該只將此屬性設定為唯一識別每個資料列所需的欄位或欄位;例如,如果整數值可唯一識別每個資料列,則為 ID 資料行。 您必須設定 DataKeyNames 屬性,才能讓控制項的 GridView 自動更新和刪除功能運作。 這些索引鍵欄位的值會傳遞至資料來源控制項,以指定要更新或刪除的資料列。
如果您需要在更新或刪除資料列時擷取資料索引鍵值,請使用 Keys
或 GridViewDeleteEventArgs 類別的 GridViewUpdateEventArgs 屬性。 例如, e.Keys[0]
保留 或 RowDeleting 事件處理常式中 RowUpdating 第一個資料索引鍵的值。
如果您需要在選取資料列時擷取資料鍵值,請使用 SelectedDataKey 屬性。
DataKeyNames設定屬性時, GridView 控制項會自動使用指定欄位或欄位的值填入其 DataKeys 集合,以方便存取每個資料列的主鍵。
注意
控制項會將 GridView 這些索引鍵域值儲存在控制項狀態中。 如果這些值包含敏感性資訊,強烈建議您將 屬性設定 ViewStateEncryptionMode 為 ViewStateEncryptionMode.Always
來啟用檢視狀態加密。
當您藉由將 屬性設定 AutoGenerateColumns 為 true
) 來使用自動產生的欄位資料行 (時, GridView 控制項可確保對應至 屬性中所 DataKeyNames 指定欄位或欄位的資料行是唯讀的。
如果資料 Visible 列欄位的 屬性設定 false
為 ,則資料行不會顯示在 控制項中 GridView ,而且資料行的資料不會往返用戶端。 如果您想要讓用戶端看不見的資料行資料,請將功能變數名稱新增至 DataKeyNames 屬性。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應