The DataList, DataGrid, Repeater, and plain old HTML can be used to create some advanced presentations in ASP.NET. In this article—a continuation of a three-part article—I will demonstrate how to intersperse HTML table rows and cells across DataList templates to intimately manage the appearance of a DataList. In addition, I will demonstrate how to nest a DataGrid in a DataList, and the best way to manage the presentation and code-behind for control nesting by using a UserControl.
All of the skills in the previous article and this article are beneficial precursors for creating presentation layers that easily model complex data relationships. After you have read the first two articles, you will be prepared for the third, and for implementing the most complex graphical user interfaces for the Web. All that you will need to bring to the table is a modicum of visual good taste. (Unfortunately, this is something that my left brain is not prepared to teach.)
Spanning an HTML Table Across Template Sections
A DataGrid is approximately an advanced form of an HTML table that coordinates the vertical and horizontal alignment of data, including data that plays the role of header and footer information with data that comprises the content. A symmetric appearance, while not especially artistic, does lend itself to good order. There are times that you may not necessarily want to use a DataGrid but want to constrain the appearance of data. For example, the Repeater is a faster, read-only control that displays collections of data like the DataGrid, and the DataList is a read-write control that behaves like the Repeater; it repeats template-described content.
If you choose to use the DataList or Repeater, but want grid-like organization, you can intersperse the rows and cells of an HTML table across the template regions of a DataList or Repeater. The visual result is that values in the header and footer sections can be made to align themselves with related values in the selected, item, and edit sections of the DataList or Repeater. (We'll demonstrate with a DataList. Simply keep in mind that the demonstrated technique can be applied to the Repeater control too.)
The Road Less Taken
New developers may likely be more comfortable with the very visually oriented aspect of ASP.NET design. Slightly older commandos may be more comfortable off the visual designer path, writing HTML. If you are comfortable writing HTML, you will be right at home here. If you are unfamiliar or uncomfortable writing HTML, this may be a good opportunity to broaden your skills.
The scenario for our example is to use a DataList and HTML table, and the DataList's <HeaderTemplate>, <ItemTemplate>, <SelectedItemTemplate>, and <FooterTemplate> to create a coordinated grid-like appearance. Header information will be conformed to vertically align with associated data in the item and selected-item rows, and the footer will be employed to create a simple visual effect. The result will look like a grid. The code is shown in Listing 1, and the result is shown in Figure 1. The listing is followed by an explanation.
Listing 1: Demonstrating an HTML table interwoven across template sections of a DataList to create a grid-like appearance.
Listing 1 uses highlighting colors (color being one of the benefits of Web publishing) to illustrate how the HTML table in yellow is intertwined with the DataList template sections in green. The green sections include the template regions, such as the <ItemTemplate></ItemTemplate> beginning on line 65 and ending on line 94. The yellow sections show HTML tags, such as the table row beginning on line 66 and ending on line 87. (Lines 66 to 87 and 88 to 93 create the unselected data rows (see Figure 1.) The total effect is illustrated in Figure 1. The biggest drawback of combining an HTML table and DataList is that you must create the result by writing HTML. The designer seems incapable of rendering the effect at design time.
Figure 1: A DataList whose appearance is constrained by an HTML table.
By using an HTML table and a DataList, I was able to use one column and two rows for the image and just one row and column for each of the remaining data values, while keeping column headers lined up with column and row data. With a little effort, we could create a style sheet and (getting some artistic feedback), improve the total effect.
One might consider defining a UserControl with the HTML table and placing the UserControl in the DataList's ItemTemplate. The drawback with this approach is that the whole table is repeated for each row of data. By melding the HTML table and DataList controls together, the header is displayed once and just the data rows are repeated, as shown in Figure 1.