DataKey クラス
アセンブリ: System.Web (system.web.dll 内)


DataKey クラスは、データ バインド コントロール内のレコードの主キーを表すために使用されます。レコードの主キーは、データ ソースの 1 つ以上のフィールドで構成できます。DataKey クラスはコレクションではありませんが、キー フィールドの値を複数格納できます。DataKey クラスのコンストラクタの 1 つが呼び出されると、キー フィールドの値が代入されます。DataKey オブジェクトからキー フィールドの値を取得する方法は、次のとおりです。
-
DataKey.Item(Int32) プロパティを使用して、DataKey オブジェクト内の特定のインデックス位置にあるキー フィールドの値を取得します。
-
Value プロパティを使用して、DataKey オブジェクト内のインデックス 0 にあるキー フィールドの値を取得します。このプロパティは、主キーにフィールドが 1 つしかない場合に、レコードのキー値を取得するためのショートカットとして使用されます。
-
Values プロパティを使用して、キー フィールドの値を反復処理するために使用できる IOrderedDictionary オブジェクトを作成します。
通常、DataKey オブジェクトは、データ バインド コントロールの DataKeyNames プロパティが設定されている場合にそのコントロールによって自動生成されます。DataKey オブジェクトには、DataKeyNames プロパティ内で指定された 1 つ以上のキー フィールドの値が格納されます。一度に 1 つのレコードだけを表示するデータ バインド コントロール (DetailsView または FormView など) には、通常、そのコントロールの DataKey プロパティ内に現在表示されているレコードの DataKey オブジェクトが格納されます。一度に複数のレコードを表示するデータ バインド コントロール (GridView など) には、通常、DataKeyArray コレクション内にあるこのコントロールの各レコードの DataKey オブジェクトが格納されます。その後、DataKeyArray コレクションがコントロールの DataKeys プロパティに格納されます。

DataKey オブジェクトの Value プロパティを使用して DetailsView コントロール内のレコードの主キーの値を確認するコード例を次に示します。
<%@ Page language="VB" %> <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 the value of the key field. MessageLabel.Text = "The key field value for the displayed record is " & _ key.Value.ToString() & "." End Sub </script> <html> <body> <form 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>
<%@ Page language="C#" %> <script runat="server"> void CustomerDetailsView_DataBound(Object sender, EventArgs e) { // Get the DataKey object for the current record. DataKey key = CustomerDetailsView.DataKey; // Display the the value of the key field. MessageLabel.Text = "The key field value for the displayed record is " + key.Value.ToString() + "."; } </script> <html> <body> <form 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>


System.Web.UI.WebControls.DataKey


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataKey クラスのページへのリンク