GridView.DataKeys 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public:
virtual property System::Web::UI::WebControls::DataKeyArray ^ DataKeys { System::Web::UI::WebControls::DataKeyArray ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataKeyArray DataKeys { get; }
[<System.ComponentModel.Browsable(false)>]
member this.DataKeys : System.Web.UI.WebControls.DataKeyArray
Public Overridable ReadOnly Property DataKeys As DataKeyArray
屬性值
DataKeyArray,包含 GridView 控制項中每個資料列的資料索引鍵。
- 屬性
範例
下列範例示範如何使用 DataKeys 屬性來判斷 控制項中 GridView 所選資料列的資料索引鍵值。
<%@ 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 CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Determine the index of the selected row.
int index = CustomersGridView.SelectedIndex;
// Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " +
CustomersGridView.DataKeys[index].Value.ToString() + ".";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView DataKeys Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView DataKeys Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
</asp:gridview>
<!-- 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="CustomersSource"
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 CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Determine the index of the selected row.
Dim index As Integer = CustomersGridView.SelectedIndex
' Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " & _
CustomersGridView.DataKeys(index).Value.ToString() & "."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView DataKeys Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView DataKeys Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
</asp:gridview>
<!-- 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="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
備註
DataKeyNames設定 屬性時, GridView 控制項會自動為 控制項中的每個資料列建立 DataKey 物件。 物件 DataKey 包含 屬性中指定的 DataKeyNames 欄位或欄位值。 然後,物件 DataKey 會加入至控制項的 DataKeys 集合。 DataKeys使用 屬性來擷取 DataKey 控制項中特定資料列的物件 GridView 。
注意
您可以使用 SelectedDataKey 屬性來擷取 DataKey 目前選取之資料列的物件。 您也可以使用 SelectedValue 屬性,直接擷取目前選取資料列的資料索引鍵值。