DetailsView.Fields Property

Gets a collection of DataControlField objects that represent the explicitly declared row fields in a DetailsView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
public virtual DataControlFieldCollection Fields { get; }

Property Value

Type: System.Web.UI.WebControls.DataControlFieldCollection
A DataControlFieldCollection that contains all explicitly declared row fields in the DetailsView control.

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.

NoteNote:

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.

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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft