Conclusion

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"> <itemtemplate> HTML and/or ASP.NET layout code </itemtemplate> </asp:TemplateColumn>

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.

Table 3-1 Column Template Properties Supported by the DataGrid Control

Template Property

Usage

ItemTemplate

The template for the items in a DataGrid control s column.

<ItemTemplate> <asp:label runat="server" text= '<%# ... %>' /> </ItemTemplate>

You can use any combination of HTML and ASP.NET controls to populate the column.

EditItemTemplate

Controls the contents of the item selected for editing in the DataGrid control s column.

<EditItemTemplate> <asp:textbox runat="server" text= '<%# ... %>' /> </EditItemTemplate>

HeaderTemplate

Contains information for the heading section.

<HeaderTemplate> <asp:label runat="server" text= "Header" /> </HeaderTemplate>

If you omit this template, the column header is rendered with a label or with a link if sorting is enabled. When you specify a custom template, you are responsible for providing the user interface tools to enable sorting on the column.

FooterTemplate

Contains information for the footer section of the column. This property value is null by default.

<FooterTemplate> <asp:label runat="server" text= "..." /> </FooterTemplate>

The footer is displayed only if the ShowFooter property of the DataGrid control is set to true.

caution

Unless you need special functionality, you are better off leaving the header template as is. Changing the template is an easy and smooth process as long as you don t need to establish a direct interaction between the elements in the header and the user. More on this in a moment.

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.



Building Web Solutions with ASP. NET and ADO. NET
Building Web Solutions with ASP.Net and ADO.NET
ISBN: 0735615780
EAN: 2147483647
Year: 2002
Pages: 75
Authors: Dino Esposito

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net