DetailsView.DataKey プロパティ
アセンブリ: System.Web (system.web.dll 内)


DataKeyNames プロパティが設定されている場合、DetailsView コントロールは、現在のレコードのキー フィールドを表す DataKey オブジェクトを自動的に作成します。この DataKey オブジェクトを取得するには、DataKey プロパティを使用します。このプロパティは、通常、DataKeyNames プロパティで指定されているフィールドのキーと、それらに対応する値を取得するために使用されます。
![]() |
---|
より簡単な方法として、SelectedValue プロパティを使用して、DataKeyNames プロパティにリストされている最初のキー フィールドの値を確認することもできます。 |

DataKey プロパティを使用して、現在のレコードのキー フィールドと対応する値を確認する方法のコード例を次に示します。
<%@ Page Language="VB" %> <script runat="server"> Sub CustomerDetailView_ItemUpdated(ByVal sender As Object, _ ByVal e As DetailsViewUpdatedEventArgs) ' Create a dictionary that contains the key fields and values ' using the AllValues method of the DataKey object contained ' in the DataKey property. Dim keyList As IOrderedDictionary = _ CustomerDetailsView.DataKey.Values ' Get the ArrayList objects that represent the key fields ' and values. Dim keys As ArrayList = CType(keyList.Keys, ArrayList) Dim values As ArrayList = CType(keyList.Values, ArrayList) ' Get the key field and value for the current record. Dim keyField As String = keys(0).ToString() Dim keyValue As String = values(0).ToString() ' Log the update operation using the key field and value. LogUpdate(keyField, keyValue) End Sub Sub LogUpdate(ByVal keyField As String, ByVal keyValue As String) ' Insert code to log the update operation. End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3> DetailsView DataKey Example</h3> <asp:DetailsView ID="CustomerDetailsView" DataSourceID="DetailsViewSource" DataKeyNames="CustomerID" AutoGenerateRows="true" AutoGenerateEditButton="true" AllowPaging="true" OnItemUpdated="CustomerDetailView_ItemUpdated" 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]" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [Address] = @Address, [City] = @City, [PostalCode] = @PostalCode, [Country] = @Country WHERE [CustomerID] = @CustomerID"></asp:SqlDataSource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void CustomerDetailView_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e) { // Create a dictionary that contains the key fields and values using // the AllValues method of the DataKey object contained in the DataKey // property. IOrderedDictionary keyList = CustomerDetailView.DataKey.Values; // Get the ArrayList objects that represent the key fields and values. ArrayList keys = (ArrayList)keyList.Keys; ArrayList values = (ArrayList)keyList.Values; // Get the key field and value for the current record. String keyField = keys[0].ToString(); String keyValue = values[0].ToString(); // Log the update operation using the key field and value. LogUpdate(keyField, keyValue); } void LogUpdate(String keyField, String keyValue) { // Insert code to log the update operation. } </script> <html> <body> <form id="Form1" runat="server"> <h3>DetailsView DataKey Example</h3> <asp:detailsview id="CustomerDetailView" datasourceid="DetailsViewSource" datakeynames="CustomerID" autogeneraterows="true" autogenerateeditbutton="true" allowpaging="true" onitemupdated="CustomerDetailView_ItemUpdated" 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]" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [Address] = @Address, [City] = @City, [PostalCode] = @PostalCode, [Country] = @Country WHERE [CustomerID] = @CustomerID"> </asp:SqlDataSource> </form> </body> </html>

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDetailsView.DataKey プロパティを検索する場合は、下記のリンクをクリックしてください。

- DetailsView.DataKey プロパティのページへのリンク