Modifier

Partager via


DetailsView.Fields Property

Definition

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; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.Fields : System.Web.UI.WebControls.DataControlFieldCollection
Public Overridable ReadOnly Property Fields As DataControlFieldCollection

Property Value

A DataControlFieldCollection that contains all explicitly declared row fields in the DetailsView control.

Attributes

Examples

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>

Remarks

When you explicitly declare the row fields for a DetailsView control, these row fields are stored in the Fields property (collection). The Fields collection also allows you to programmatically manage the collection of explicitly declared rows.

Note

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. Automatically generated row fields are not added to the Fields collection.

Different row field types determine the behavior of the rows in the control. 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 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 Displays a check box in the DetailsView control. This row field type is commonly used to display fields with a Boolean value.
CommandField 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. This row field type allows you to bind a second field to the hyperlink's URL.
ImageField Displays an image in the DetailsView control.
TemplateField 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.

To explicitly declare the row fields for a DetailsView control, first set the AutoGenerateRows property to false. Next, add opening and closing <Fields> tags between the opening and closing tags of the DetailsView control. Finally, list the row fields that you want to include between the opening and closing <Fields> tags. The row fields are displayed in the DetailsView control in the order that the row fields appear in the Fields collection.

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.

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. 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.

Applies to

See also