DetailsView.Fields 属性

定义

获取 DataControlField 对象的集合,这些对象表示 DetailsView 控件中显式声明的行字段。Gets a collection of DataControlField objects that represent the explicitly declared row fields in a DetailsView control.

public:
 virtual property System::Web::UI::WebControls::DataControlFieldCollection ^ Fields { System::Web::UI::WebControls::DataControlFieldCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.DataControlFieldCollection Fields { get; }
member this.Fields : System.Web.UI.WebControls.DataControlFieldCollection
Public Overridable ReadOnly Property Fields As DataControlFieldCollection

属性值

一个 DataControlFieldCollection,包含 DetailsView 控件中所有显式声明的行字段。A DataControlFieldCollection that contains all explicitly declared row fields in the DetailsView control.

属性

示例

下面的代码示例演示如何以声明方式将行字段添加FieldsDetailsView控件的集合。The following code example demonstrates how to declaratively add row fields to the Fields collection of a 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">
<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView Fields Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView Fields Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          AutoGenerateRows="false"
          allowpaging="true"
          runat="server">
               
        <Fields>
         <asp:BoundField 
           DataField="CompanyName" 
           HeaderText="Company Name"/>
         <asp:BoundField 
           DataField="City" 
           HeaderText="City"/>
        </Fields>
                    
        </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">
<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView Fields Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView Fields Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          AutoGenerateRows="false"
          allowpaging="true"
          runat="server">
               
        <Fields>
         <asp:BoundField 
           DataField="CompanyName" 
           HeaderText="Company Name"/>
         <asp:BoundField 
           DataField="City" 
           HeaderText="City"/>
        </Fields>
                    
        </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>

注解

显式声明DetailsView控件的行字段时, 这些行字段存储Fields在属性 (集合) 中。When you explicitly declare the row fields for a DetailsView control, these row fields are stored in the Fields property (collection). Fields集合还允许以编程方式管理显式声明的行的集合。The Fields collection also allows you to programmatically manage the collection of explicitly declared rows.

备注

显式声明的行字段可与自动生成的行字段结合使用。Explicitly declared row fields can be used in combination with automatically generated row fields. 如果同时使用这两种方法, 将首先呈现显式声明的行字段, 然后呈现自动生成的行字段。When both are used, explicitly declared row fields are rendered first, followed by the automatically generated row fields. 自动生成的Fields行字段不会添加到集合中。Automatically generated row fields are not added to the Fields collection.

不同的行字段类型确定控件中行的行为。Different row field types determine the behavior of the rows in the control. 下表显示了可以在Fields集合中使用的不同行字段类型。The following table shows the different row field types that can be used in the Fields collection.

行字段类型Row field type 说明Description
BoundField 以文本形式显示数据源中字段的值。Displays the value of a field in a data source as text.
ButtonField DetailsView控件中显示命令按钮。Displays a command button in the DetailsView control. 这允许您显示具有自定义按钮控件的行, 如 "添加" 或 "移除" 按钮。This allows you to display a row with a custom button control, such as an Add or a Remove button.
CheckBoxField DetailsView控件中显示复选框。Displays a check box in the DetailsView control. 此行字段类型通常用于显示具有布尔值的字段。This row field type is commonly used to display fields with a Boolean value.
CommandField 显示内置命令按钮, 用于在DetailsView控件中执行编辑、插入或删除操作。Displays built-in command buttons to perform edit, insert, or delete operations in the DetailsView control.
HyperLinkField 将数据源中的字段值显示为超链接。Displays the value of a field in a data source as a hyperlink. 此行字段类型允许您将第二个字段绑定到超链接的 URL。This row field type allows you to bind a second field to the hyperlink's URL.
ImageField DetailsView控件中显示图像。Displays an image in the DetailsView control.
TemplateField 根据指定的模板, 在DetailsView控件中显示行的用户定义内容。Displays user-defined content for a row in the DetailsView control according to a specified template. 此行字段类型允许您创建自定义行字段。This row field type allows you to create a custom row field.

若要显式声明DetailsView控件的行字段, 请首先AutoGenerateRows将属性设置为falseTo explicitly declare the row fields for a DetailsView control, first set the AutoGenerateRows property to false. 接下来, 在<Fields> DetailsView控件的开始标记和结束标记之间添加开始标记和结束标记。Next, add opening and closing <Fields> tags between the opening and closing tags of the DetailsView control. 最后, 列出要在开始标记和结束<Fields>标记之间包括的行字段。Finally, list the row fields that you want to include between the opening and closing <Fields> tags. 行字段在DetailsView控件中的显示顺序与行字段在Fields集合中的显示顺序相同。The row fields are displayed in the DetailsView control in the order that the row fields appear in the Fields collection.

尽管可以通过编程方式向Fields集合添加行字段, 但DetailsView在控件中以声明方式列出行字段并使用每个行字段Visible的属性来显示或隐藏行字段更为容易。Although you can programmatically add row fields to the Fields collection, it is easier to list the row fields declaratively in the DetailsView control and then use the Visible property of each row field to show or hide the row field.

如果行字段的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.

适用于

另请参阅