May 5, 2011, 11:46 a.m.
posted by magedrx
Showing One Record at a Time with the DetailsView
The GridView control shows all of the records from its data source control at once. Sometimes, however, we may want to show just one record at a time. The DetailsView control provides this functionality. Like the GridView control, the DetailsView control is bound to a data source control in the same manner. Drag the DetailsView onto a page with a data source control and then, from the smart tag, associate the DetailsView with the data source control. After you assign the data source control, the DetailsView presents a vertical list of the columns in the data source control.
To familiarize ourselves with the DetailsView control, let's create a new ASP.NET page called DetailsView.aspx. As in the preceding hour, add a SqlDataSource control to the page, configuring it to select all records and columns from the Books table. Next, add a DetailsView to the page and associate it with the SqlDataSource control. After you have completed these steps, your screen should look similar to Figure.
10. A DetailsView control has been added to the page and assigned a data source control.
Take a moment to view the DetailsView in a web browser by going to the Debug menu and choosing Start Without Debugging. Figure shows Internet Explorer when viewing DetailsView.aspx. Note that the first record returned by the SqlDataSource control is displayed.
11. A single record from the data source control is displayed.
You may have noticed that the DetailsView, by default, doesn't provide any mechanism by which to view the next record in the data source. That is, with the DetailsView we see one record at a time, but there's no way to move to the next record. To provide a means to step through the records, check the Enable Paging check box in the DetailsView's smart tag. This will add a pager row to the bottom of the DetailsView. The pager row contains the user interface for stepping through the records of the corresponding data source control. By default, the paging interface uses page numbers to allow the user to jump to a particular record.
After checking the Enable Paging check box, take a moment to view the ASP.NET page in a browser. Note that now there are five links at the bottom of the DetailsView, allowing you to navigate to any of the five records in the Books table by clicking on the appropriately numbered link.
Customizing the Paging Interface
The DetailsView control's pager row can be customized through the Properties window. First, click on the DetailsView in the Design view to load the properties. Next, scroll down to the Paging section of the Properties window. There, you'll see two properties:
Let's change the PagerSettings property so that instead of the page number links, we use next, previous, first, and last links. Expand the PagerSettings property and go to the Mode subproperty. Here, you'll find a drop-down list with the various choices. By default, the Numeric option is selected; change this to the NextPreviousFirstLast option. Note how the DetailsView changes in the Design view in Visual Web Developer to include the next, previous, first, and last links. Figure shows this pageable DetailsView when viewed through a browser.
Figure. The DetailsView now supports paging and is showing information for the book The Number.
Customizing the Appearance of the DetailsView
The DetailsView's appearance can be customized just like the GridView's. There are DetailsView-level, row-level, and field-level formatting properties. The DetailsView-level and row-level properties can be configured through the Properties window in the Appearance and Styles sections, and the field-level properties are accessible through the smart tag's Edit Fields link, which displays theyou guessed itFields dialog box. The DetailsView also has an Auto Format option in its smart tag, just like the GridView.
While the process of customizing the formatting of the GridView and DetailsView is identical, there are some differences between the two controls' formatting properties. For example, the DetailsView does not have the notion of a "selected" row, so there's no SelectedRowStyle. As we'll see in the next hour, the DetailsView can be used to insert data into a database, and therefore it has an InsertRowStyle property.
For practice, take a moment to customize the appearance of the DetailsView. Go ahead and customize its fields like we did with the GridView. Add some color and font settings to liven up the web page. Figure shows the DetailsView in a browser after gussying up the appearance. Note that I applied the same field-level customizations that we did with the GridView (renaming the header rows, moving the Price field to come after the Authors field, formatting the Price as a currency, and so on).
13. With some formatting, the DetailsView is much easier on the eyes.
A Look at the DetailsView's Markup
When examining the GridView earlier in this hour, we took time to examine both the GridView's declarative markup in the Source view through Visual Web Developer and the rendered HTML that is sent to the web browser. Whenever you are working with a new Web control, I encourage you to explore both the control's declarative and HTML markup because doing so helps foster a better understanding as to how these controls are represented in the ASP.NET page and in the browser.
We're not going to spend the time to explore the DetailsView's markup in earnest here; that is a task that you can handle on your own. However, I do want to point out that the DetailsView is rendered as an HTML <table>. Instead of having a table row for each record in the associated data source control and a table cell for each column of each row, the DetailsView has a table row for each column of the data source control with two table cells per table row: one for the data source control's column's name and one for its value. The result is a two-columned HTML <table> that has one row for each database table column returned by the data source control.
Listing 15.3. The DetailsView Control Is Rendered as an HTML <table>