Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   I want my samples in...      

ASP.NET Quickstart Tutorials

DetailsView

DetailsView is a data-bound user interface control that renders a single record at a time from its associated data source, optionally providing paging buttons to navigate between records. It is similar to the Form View of an Access database, and is typically used for updating and/or inserting new records. It is often used in a master-details scenario where the selected record of the master control (GridView, for example) determines the DetailsView display record.

VB Master-Details w/ GridView and DetailsView
Run Sample View Source

DetailsView supports editing just like GridView, and you can enable the UI the same way using the AutoGenerateEditButton or CommandField.ShowEditButton properties. Of course, the data source associated to the DetailsView must also be configured to support the Update operation (in this case, by specifying an UpdateCommand on SqlDataSource). The following example demonstrates a DetailsView configured to support editing records in a master-details scenario.

VB DetailsView Editing
Run Sample View Source

Normally data-bound controls automatically re-bind to their data source whenever the data source changes (for example after an update). However, in the preceding example, the DetailsView is bound to a different data source than the GridView, so when the Update operation is invoked, only the DetailsView receives a change event from its data source. To force the GridView to also re-bind when the DetailsView performs an update, you can call DataBind() explicitly on GridView in the DetailsView ItemUpdated event. This example also handles events to disable editing when a GridView sorting or paging operation occurs or when a new filter value is selected in the DropDownList control.

This next example demonstrates a master-details scenario using GridView and DetailsView in separate pages. Unlike the previous examples that showed the GridView and DetailsView bound to the same type of record (an author), this example shows different types of records for the master and details controls (authors and books), associated by a foreign key relationship in the underlying database. Because an author record may have more than one associated book, the DetailsView has been configured to support paging over the book records in the details page. The DetailsView also supports paging records using client-side callbacks through its EnablePagingCallbacks property (not shown in this example).

VB Master-Details (Separate Pages)
Run Sample View Source

Like the GridView control, the DetailsView control supports Updating and Deleting data through its data source. However, DetailsView also supports Inserting data, whereas GridView does not. You can easily pair a DetailsView with GridView to enable inserting records to be displayed in the GridView control.

VB Master-Details Insert
Run Sample View Source

You can place the DetailsView on a separate page to perform an Insert or Update operation. The following example shows a DetailsView configured on a separate page for Insert and Updates. Notice that the DefaultMode property is set in this example to Insert or Edit so that the DetailsView renders in this mode initially instead of read-only mode. After an Insert or Update operation is performed, DetailsView always reverts to the DefaultMode (which is ReadOnly, by default).

VB Master-Details Insert (Separate Pages)
Run Sample View Source

For more information and examples that demonstrate the use of the DetailsView control, refer to the Performing Data Access section.