System.Web.UI.WebControls.DetailsView Class

Displays the values of a single record from a data source in a table, where each data row represents a field of the record. The System.Web.UI.WebControls.DetailsView control allows you to edit, delete, and insert records.

See Also: DetailsView Members

Syntax

[System.ComponentModel.DefaultEvent("PageIndexChanging")]
[System.Web.UI.ControlValueProperty("SelectedValue")]
[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.DetailsViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.Web.UI.ToolboxData("<{0}:DetailsView runat="server" Width="125px" Height="50px"></{0}:DetailsView>")]
[System.Web.UI.SupportsEventValidation]
public class DetailsView : CompositeDataBoundControl, System.Web.UI.ICallbackEventHandler, System.Web.UI.IDataItemContainer, System.Web.UI.IPostBackEventHandler, ICallbackContainer, IPostBackContainer

Remarks

In this topic:

Introduction

The System.Web.UI.WebControls.DetailsView control is used to display a single record from a data source in a table, where each field of the record is displayed in a row of the table. It can be used in combination with a System.Web.UI.WebControls.GridView control for master-detail scenarios. The System.Web.UI.WebControls.DetailsView control supports the following features:

Row Fields

Each data row in the System.Web.UI.WebControls.DetailsView control is created by declaring a field control. Different row field types determine the behavior of the rows in the control. Field controls derive from System.Web.UI.WebControls.DataControlField. The following table lists the different row field types that can be used.

System.Web.UI.WebControls.BoundField

Displays the value of a field in a data source as text.

System.Web.UI.WebControls.ButtonField

Displays a command button in the System.Web.UI.WebControls.DetailsView control. This allows you to display a row with a custom button control, such as an Add or a Remove button.

System.Web.UI.WebControls.CheckBoxField

Displays a check box in the System.Web.UI.WebControls.DetailsView control. This row field type is commonly used to display fields with a Boolean value.

System.Web.UI.WebControls.CommandField

Displays built-in command buttons to perform edit, insert, or delete operations in the System.Web.UI.WebControls.DetailsView control.

System.Web.UI.WebControls.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.

System.Web.UI.WebControls.ImageField

Displays an image in the System.Web.UI.WebControls.DetailsView control.

System.Web.UI.WebControls.TemplateField

Displays user-defined content for a row in the System.Web.UI.WebControls.DetailsView control according to a specified template. This row field type allows you to create a custom row field.

By default, the DetailsView.AutoGenerateRows property is set to true, which automatically generates a bound row field object for each field of a bindable type in the data source. Valid bindable types are string, DateTime, decimal, Guid, and the set of primitive types. Each field is then displayed in a row as text, in the order in which each field appears in the data source.

Automatically generating the rows provides a quick and easy way to display every field in the record. However, to make use of the System.Web.UI.WebControls.DetailsView control's advanced capabilities you must explicitly declare the row fields to include in the System.Web.UI.WebControls.DetailsView control. To declare the row fields, first set the DetailsView.AutoGenerateRows property to false. Next, add opening and closing <Fields> tags between the opening and closing tags of the System.Web.UI.WebControls.DetailsView control. Finally, list the row fields that you want to include between the opening and closing <Fields> tags. The row fields specified are added to the DetailsView.Fields collection in the order listed. The DetailsView.Fields collection allows you to programmatically manage the row fields in the System.Web.UI.WebControls.DetailsView control.

Note:

Automatically generated row fields are not added to the DetailsView.Fields collection.

Note:

Explicitly declared row fields can be displayed 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.

Binding to Data

The System.Web.UI.WebControls.DetailsView control can be bound to a data source control (such as the System.Web.UI.WebControls.SqlDataSource control or System.Web.UI.WebControls.ObjectDataSource control) or to any data source collection that implements the IEnumerable interface, such as System.Data.DataView, ArrayList, List`1, or other collection types. Use one of the following methods to bind the System.Web.UI.WebControls.DetailsView control to the appropriate data source type:

For more information on data binding, see ASP.NET Data Binding.

Security

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Script Exploits Overview. Validation server controls are also provided to assess user input. For more information, see Validation Server Controls.

Data Operations

The System.Web.UI.WebControls.DetailsView control provides built-in capabilities that allow the user to update, delete, insert, and page through items in the control. When the System.Web.UI.WebControls.DetailsView control is bound to a data source control, the System.Web.UI.WebControls.DetailsView control can take advantage of the data source control's capabilities and provide automatic updating, deleting, inserting, and paging functionality.

Note:

The System.Web.UI.WebControls.DetailsView control can provide support for update, delete, insert, and paging operations with other types of data sources; however, you must provide the implementation for these operations in an appropriate event handler. For more information, see DetailsView.ItemDeleting, DetailsView.ItemInserting, and DetailsView.ItemUpdating.

The System.Web.UI.WebControls.DetailsView control can automatically add a System.Web.UI.WebControls.CommandField row field with an Edit, Delete, or New button by setting the DetailsView.AutoGenerateEditButton, DetailsView.AutoGenerateDeleteButton, or DetailsView.AutoGenerateInsertButton properties to true, respectively. Unlike the Delete button (which deletes the selected record immediately), when the Edit or New button is clicked, the System.Web.UI.WebControls.DetailsView control goes into edit or insert mode, respectively. In edit mode, the Edit button is replaced with an Update and a Cancel button. Input controls that are appropriate for the field's data type (such as a System.Web.UI.WebControls.TextBox or a System.Web.UI.WebControls.CheckBox control) are displayed with a field's value for the user to modify. Clicking the Update button updates the record in the data source, while clicking the Cancel button abandons any changes. Likewise, in insert mode, the New button is replaced with an Insert and a Cancel button, and empty input controls are displayed for the user to enter the values for the new record.

Note:

You can also manually define the update, delete, and insert command buttons in a System.Web.UI.WebControls.ButtonField, System.Web.UI.WebControls.CommandField, or System.Web.UI.WebControls.TemplateField row field. The System.Web.UI.WebControls.DetailsView control recognizes buttons with the Button.CommandName property set to "Edit", "Update", "Delete", "New", "Insert", or "Cancel"; however, you still must provide the functionality yourself. For more information, see DetailsView.ItemDeleting, DetailsView.ItemInserting, and DetailsView.ItemUpdating.

The System.Web.UI.WebControls.DetailsView control provides a paging feature, which allows the user to navigate to other records in the data source. When enabled, page navigation controls are displayed in a pager row. To enable paging, set the DetailsView.AllowPaging property to true. The pager row can be customized using the DetailsView.PagerStyle and DetailsView.PagerSettings properties.

Customizing the User Interface

You can customize the appearance of the System.Web.UI.WebControls.DetailsView control by setting the style properties for different parts of the control. The following table lists the style properties that you can set.

DetailsView.AlternatingRowStyle

The style settings for the alternating data rows in the System.Web.UI.WebControls.DetailsView control. When this property is set, the data rows are displayed in bands, alternating between the DetailsView.RowStyle settings and the DetailsView.AlternatingRowStyle settings.

DetailsView.CommandRowStyle

The style settings for the row that contains the built-in command buttons.

DetailsView.EditRowStyle

The style settings for the data rows when the System.Web.UI.WebControls.DetailsView control is in edit mode.

DetailsView.EmptyDataRowStyle

The style settings for the empty data row that are displayed in the System.Web.UI.WebControls.DetailsView control when the data source does not contain any records.

DetailsView.FooterStyle

The style settings for the footer row.

DetailsView.HeaderStyle

The style settings for the header row.

DetailsView.InsertRowStyle

The style settings for the data rows when the System.Web.UI.WebControls.DetailsView control is in insert mode.

DetailsView.PagerStyle

The style settings for the pager row.

DetailsView.RowStyle

The style settings for the data rows in the System.Web.UI.WebControls.DetailsView control. When the DetailsView.AlternatingRowStyle property is also set, the data rows are displayed alternating between the DetailsView.RowStyle settings and the DetailsView.AlternatingRowStyle settings.

DetailsView.FieldHeaderStyle

The style settings for the header column.

Events

The System.Web.UI.WebControls.DetailsView control provides several events that you can program against. This allows you to run a custom routine whenever an event occurs. The following table lists the events supported by the System.Web.UI.WebControls.DetailsView control. The System.Web.UI.WebControls.DetailsView control also inherits these events from its base classes: System.Web.UI.Control.DataBinding, BaseDataBoundControl.DataBound, System.Web.UI.Control.Disposed, System.Web.UI.Control.Init, System.Web.UI.Control.Load, System.Web.UI.Control.PreRender, and System.Web.UI.Control.Render(System.Web.UI.HtmlTextWriter).

DetailsView.ItemCommand

Occurs when a button is clicked in the System.Web.UI.WebControls.DetailsView control.

DetailsView.ItemCreated

Occurs after all System.Web.UI.WebControls.DetailsViewRow objects are created in the System.Web.UI.WebControls.DetailsView control. This event is often used to modify the values of a record before it is displayed.

DetailsView.ItemDeleted

Occurs when a Delete button is clicked, but after the System.Web.UI.WebControls.DetailsView control deletes the record from the data source. This event is often used to check the results of the delete operation.

DetailsView.ItemDeleting

Occurs when a Delete button is clicked, but before the System.Web.UI.WebControls.DetailsView control deletes the record from the data source. This event is often used to cancel the delete operation.

DetailsView.ItemInserted

Occurs when an Insert button is clicked, but after the System.Web.UI.WebControls.DetailsView control inserts the record. This event is often used to check the results of the insert operation.

DetailsView.ItemInserting

Occurs when an Insert button is clicked, but before the System.Web.UI.WebControls.DetailsView control inserts the record. This event is often used to cancel the insert operation.

DetailsView.ItemUpdated

Occurs when an Update button is clicked, but after the System.Web.UI.WebControls.DetailsView control updates the row. This event is often used to check the results of the update operation.

DetailsView.ItemUpdating

Occurs when an Update button is clicked, but before the System.Web.UI.WebControls.DetailsView control updates the record. This event is often used to cancel the update operation.

DetailsView.ModeChanged

Occurs after the System.Web.UI.WebControls.DetailsView control changes modes (edit, insert, or read-only mode). This event is often used to perform a task when the System.Web.UI.WebControls.DetailsView control changes modes.

DetailsView.ModeChanging

Occurs before the System.Web.UI.WebControls.DetailsView control changes modes (edit, insert, or read-only mode). This event is often used to cancel a mode change.

DetailsView.PageIndexChanged

Occurs when one of the pager buttons is clicked, but after the System.Web.UI.WebControls.DetailsView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different record in the control.

DetailsView.PageIndexChanging

Occurs when one of the pager buttons is clicked, but before the System.Web.UI.WebControls.DetailsView control handles the paging operation. This event is often used to cancel the paging operation.

Accessibility

For information about how to configure this control so that it generates markup that conforms to accessibility standards, see Accessibility in Visual Studio 2010 and ASP.NET 4 and ASP.NET Controls and Accessibility.

Declarative Syntax

Example

<asp:DetailsView
    AccessKey="string"
    AllowPaging="True|

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0