DataKeyArray クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説DataKeyArray クラスは、DataKey オブジェクトのコレクションの格納と管理に使用されます。DataKey オブジェクトは、データ バインド コントロール内のレコードの主キーを表します。通常、複数のレコードを表示するデータ バインド コントロール (GridView コントロールなど) では、DataKeyArray オブジェクトを使用して、コントロールに表示されるレコードの DataKey オブジェクトを格納します。
使用例インデクサを使用して DataKeyArray コレクションから DataKey オブジェクトを取得する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound ' Use the indexer to retrieve the DataKey object for the ' first record. Dim key As DataKey = CustomerGridView.DataKeys(0) ' Display the the value of the primary key for the first ' record displayed in the GridView control. MessageLabel.Text = "The primary key of the first record displayed is " & _ key.Value.ToString() & "." End Sub </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" runat="server"> </asp:gridview> <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="CustomerDataSource" 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 CustomerGridView_DataBound(Object sender, EventArgs e) { // Use the indexer to retrieve the DataKey object for the // first record. DataKey key = CustomerGridView.DataKeys[0]; // Display the the value of the primary key for the first // record displayed in the GridView control. MessageLabel.Text = "The primary key of the first record displayed is " + key.Value.ToString() + "."; } </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" ondatabound="CustomerGridView_DataBound" runat="server"> </asp:gridview> <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="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
DataKeyArray コレクションを反復処理する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound ' Display the the value of the primary key for each ' record in the GridView control. MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>" Dim key As DataKey For Each key In CustomerGridView.DataKeys MessageLabel.Text += key.Value.ToString() + "<br/>" Next End Sub </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" runat="server"> </asp:gridview> <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="CustomerDataSource" 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 CustomerGridView_DataBound(Object sender, EventArgs e) { // Display the the value of the primary key for each // record in the GridView control. MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>"; foreach (DataKey key in CustomerGridView.DataKeys) { MessageLabel.Text += key.Value.ToString() + "<br/>"; } } </script> <html> <body> <form runat="server"> <h3>DataKeyArray Example</h3> <asp:gridview id="CustomerGridView" datasourceid="CustomerDataSource" autogeneratecolumns="true" datakeynames="CustomerID" allowpaging="true" ondatabound="CustomerGridView_DataBound" runat="server"> </asp:gridview> <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="CustomerDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
.NET Framework のセキュリティ
継承階層System.Web.UI.WebControls.DataKeyArray
スレッド セーフ
プラットフォーム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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照- DataKeyArray クラスのページへのリンク