Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DataKey Property

FormView.DataKey Property

Note: This property is new in the .NET Framework version 2.0.

Gets a DataKey object that represents the primary key of the displayed record.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual DataKey DataKey { get; }
/** @property */
public DataKey get_DataKey ()

public function get DataKey () : DataKey

Property Value

A DataKey object that represents the primary key of the displayed record.

When the DataKeyNames property is set, the FormView control automatically creates a DataKey object that contains the key/value pairs of the field or fields listed in the DataKeyNames property for the current record. Use the DataKey property to get this DataKey object. This property is commonly used to obtain the keys and their corresponding values for the fields specified in the DataKeyNames property.

NoteNote

As a shortcut, you can also use the SelectedValue property to determine the value of the first key field listed in the DataKeyNames property.

The following example demonstrates how to use the DataKey property to determine the value of the key field.


<%@ 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") %>&nbsp;<%# 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft