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


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

DataKey プロパティを使用して、キー フィールドの値を確認する方法を次の例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub EmployeeFormView_ItemCreated(ByVal sender As Object, ByVal e As EventArgs) Dim key As DataKey = EmployeeFormView.DataKey MessageLabel.Text = "The key value is " & key.Value.ToString() & "." End Sub </script> <html> <body> <form runat="server"> <h3>FormView DataKey Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" onitemcreated="EmployeeFormView_ItemCreated" runat="server"> <itemtemplate> <table> <tr> <td> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td> <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3> <%# Eval("Title") %> </td> </tr> </table> </itemtemplate> </asp:formview> <br/><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="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void EmployeeFormView_ItemCreated(Object sender, EventArgs e) { DataKey key = EmployeeFormView.DataKey; MessageLabel.Text = "The key value is " + key.Value.ToString() + "."; } </script> <html> <body> <form runat="server"> <h3>FormView DataKey Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" onitemcreated="EmployeeFormView_ItemCreated" runat="server"> <itemtemplate> <table> <tr> <td> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td> <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3> <%# Eval("Title") %> </td> </tr> </table> </itemtemplate> </asp:formview> <br/><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="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </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に収録されているすべての辞書からFormView.DataKey プロパティを検索する場合は、下記のリンクをクリックしてください。

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