DataKey.Value Propriété

Définition

Obtient la valeur du champ clé à l'index 0 dans l'objet DataKey.

public:
 virtual property System::Object ^ Value { System::Object ^ get(); };
public virtual object Value { get; }
member this.Value : obj
Public Overridable ReadOnly Property Value As Object

Valeur de propriété

Valeur du champ clé à l'index 0 dans DataKey.

Exemples

L’exemple de code suivant montre comment utiliser la Value propriété pour déterminer la valeur de clé primaire d’un enregistrement.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomerDetailsView_DataBound(Object sender, EventArgs e)
  {
    // Get the DataKey object for the current record.
    DataKey key = CustomerDetailsView.DataKey;
    
    // Display the value of the key field.
    MessageLabel.Text = "The key field value for the displayed record is " + 
      key.Value.ToString() + ".";
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKey Example</title>
</head>
<body>
    <form id="form1" 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>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 value of the key field.
    MessageLabel.Text = "The key field value for the displayed record is " & _
      key.Value.ToString() & "."
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DataKey Example</title>
</head>
<body>
    <form id="form1" 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>

Remarques

Au lieu d’utiliser la Item[] propriété ou pour Values récupérer une valeur de champ de clé à partir d’un DataKey objet, vous pouvez utiliser la Value propriété comme raccourci pour déterminer la valeur du champ de clé à l’index 0 dans l’objet DataKey . Cette propriété est couramment utilisée pour déterminer la valeur du champ de clé d’un enregistrement lorsque l’enregistrement n’a qu’un seul champ de clé.

Notes

Si l’objet DataKey ne contient aucun champ clé, null est retourné.

S’applique à

Voir aussi