DetailsView.DataKeyNames 属性

定义

获取或设置一个数组,该数组包含数据源的键字段的名称。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()

属性值

String[]

一个数组,包含数据源的键字段的名称。An array that contains the names of the key fields of the data source.

实现

属性

示例

下面的代码示例演示如何使用DataKeyNames属性标识绑定DetailsView到控件的数据源的键字段。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>

注解

DataKeyNames使用属性指定以逗号分隔的字段名称列表, 这些字段名称表示数据源的主键。Use the DataKeyNames property to specify a comma-separated list of field names that represent the primary key of the data source. 设置属性后DetailsView , 控件将自动创建一个对象, DataKey该对象表示当前记录的一个或多个键字段, 并将DataKey其存储在属性中。 DataKeyNamesWhen 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. 当属性设置为true时, DetailsView控件将自动确保在DataKeyNames属性中指定的一个或多个字段是只读的。 AutoGenerateColumnsWhen 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.

备注

必须将DataKeyNames属性设置为自动更新、删除和插入DetailsView控件的功能才能工作。You must set the DataKeyNames property for the automatic updating, deleting, and inserting features of the DetailsView control to work.

如果行字段的false DetailsView属性设置为, 则不会在控件中显示行, 并且该行的数据不会对客户端进行往返。 VisibleIf 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. 如果希望不可见的行的数据进行往返, 请将字段名称添加到DataKeyNames属性。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.

适用于

另请参阅