Binding Templated Columns

Binding Templated Columns

A templated column is recognized by the <TemplateColumn> tag. The body of the tag contains from one through four different templates: ItemTemplate, Edit­ItemTemplate, HeaderTemplate, and FooterTemplate. You can tell from these names that the tag lacks a specific template for the alternating item and selected item. Alternating and selected items typically differ from other items only in terms of graphical settings such as background and foreground color and font styles, and such graphical settings can be easily set for any item at the DataGrid control level by using the <AlternatingItemStyle> and <SelectedItemStyle> tags. You will typically not need to change the layout of a column when one of its cells is selected or when it needs to be redrawn with alternating items. But when you do, for this sort of “genetic” manipulation of the grid, nothing works better than hooking the ItemCreated event.

The following code shows how to bind a templated column to a DataGrid control. Notice that a templated column, like any other column type, can have header text as well as a sorting expression. It does not, however, have an explicit data source field to bind to. Among the members of the TemplateColumn class, you will not find any DataField or DataTextField properties.

<asp:TemplateColumn runat="server" 
    HeaderText="heading" SortExpression="field">
        HTML and/or ASP.NET layout code

To bind a template column to one or more data fields, you use a data-binding expression and the DataBinder class, which was fully described in Chapter 1. To render a column, you could use a Label control that has the Text property, but you could also choose a drop-down list control (more on this later) or an image, both of which do not have anything like the Text property. As a result, you must always use data-binding expressions to bind data, which provides you unprecedented flexibility, albeit with more verbose code. The following code snippet is valid content for an item template:

<asp:label runat="server" Text='<%# 
    DataBinder.Eval(Container.DataItem, "lastname") %>' />

By using DataBinder.Eval, you can access any number of fields in the currently bound data source. In addition, you can combine them in any order to obtain an expression that would otherwise be impossible using a simple bound column or button column. The key advantage of templated columns is that any ASP.NET control can be used to populate the column’s cells. How those controls are filled is completely up to you, providing an advantage over binding to data using only the rigid DataField or DataTextField properties.

ItemTemplate is the property that lets you define the layout and the contents of each cell in the column. Other templates let you define the structure of the column header (HeaderTemplate) and footer (FooterTemplate). You can determine the behavior and appearance of the column when a cell is being edited via the EditItemTemplate template. (I’ll have more to say about in-place column editing inChapter 4.) Table 3-1 summarizes the column template properties supported by the DataGrid Web control and hints for using them.

The template properties in Table 3-1 are exposed by the TemplateColumn class as data members of a type that inherits from the ITemplate interface. Template-based properties are declaratively set with plain text in the layout of ASP.NET pages. During page processing, the ASP.NET run time takes care of loading that text into a data member of the proper type.

In addition to template properties, the TemplateColumn class provides a few style properties—ItemStyle, HeaderStyle, and FooterStyle—which you can use to customize the appearance of items in individual columns. You use these properties in the same way you use the properties for the column types discussed in Chapter 2.