DetailsView.DataKeyNames DetailsView.DataKeyNames DetailsView.DataKeyNames DetailsView.DataKeyNames Property

Definizione

Ottiene o imposta una matrice contenente i nomi dei campi chiave dell'origine dati.Gets or sets an array that contains the names of the key fields for the data source.

public:
 virtual property cli::array <System::String ^> ^ DataKeyNames { cli::array <System::String ^> ^ get(); void set(cli::array <System::String ^> ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))]
public virtual string[] DataKeyNames { get; set; }
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()

Valore della proprietà

String[]

Matrice contenente i nomi dei campi chiave dell'origine dati.An array that contains the names of the key fields of the data source.

Implementazioni

Esempi

Nell'esempio di codice riportato di seguito viene illustrato DataKeyNames come utilizzare la proprietà per identificare il campo chiave per l'origine dati DetailsView associata al controllo.The following code example demonstrates how to use the DataKeyNames property to identify the key field for the data source bound to the DetailsView control.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          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]">
        </asp:SqlDataSource>
    </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          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]">
        </asp:SqlDataSource>
    </form>
  </body>
</html>

Commenti

Utilizzare la DataKeyNames proprietà per specificare un elenco delimitato da virgole di nomi di campo che rappresentano la chiave primaria dell'origine dati.Use the DataKeyNames property to specify a comma-separated list of field names that represent the primary key of the data source. Quando viene DataKeyNames impostata la proprietà, il DetailsView controllo crea automaticamente un DataKey oggetto che rappresenta il campo o i campi chiave per DataKey il record corrente e lo archivia nella proprietà.When the DataKeyNames property is set, the DetailsView control automatically creates a DataKey object that represents the key field or fields for the current record and stores it in the DataKey property. Quando anche AutoGenerateColumns la proprietà è impostata su true, il DetailsView controllo assicura automaticamente DataKeyNames che il campo o i campi specificati nella proprietà siano di sola lettura.When the AutoGenerateColumns property is also set to true, the DetailsView control automatically ensures that the field or fields specified in the DataKeyNames property are read-only.

Nota

È necessario impostare la DataKeyNames proprietà per le funzionalità DetailsView di aggiornamento, eliminazione e inserimento automatiche del controllo per funzionare.You must set the DataKeyNames property for the automatic updating, deleting, and inserting features of the DetailsView control to work.

Se la Visible proprietà di un campo di riga è impostata falsesu, la riga non DetailsView viene visualizzata nel controllo e i dati per la riga non costituiscono un round trip al client.If the Visible property of a row field is set to false, the row is not displayed in the DetailsView control and the data for the row does not make a round trip to the client. Se si desidera che i dati di una riga che non è visibile per creare una round trip, aggiungere il nome del campo DataKeyNames alla proprietà.If you want the data for a row that is not visible to make a round trip, add the field name to the DataKeyNames property.

Si applica a

Vedi anche