Templates can be used with a DataList, to control the formatting of displayed items.
For example, suppose you want to display multiple database fields for each item displayed by the DataList. You can format the appearance of each DataList item by placing an HTML table in the ItemTemplate.
The output of the above example is shown below:
In the above example three templates are used:- an ItemTemplate, aHeaderTemplate and a SeparatorTemplate.
- HeaderTemplate displays the heading Employee Home phone numbers at the top of DataList.
- The ItemTemplate displays each author’s first name, last name and home phone number.
- The SeperatorTemplate renders a horizontal rule beween each item(just like <HR> tag of HTML)
Binding Data to the Control
As discussed earlier that to bind data to control, the DataList control must be bound to a data source. Typical data sources for the DataList are data sets or Data View and data readers (OleDbDataReaderor SqlDataReader objects). Binding can also be done to data sources created in code, such as a data reader or an array. The control can be used as a data source and any class that supports the IEnumerable interface.
When a data is bounded, a data source is specified for the DataList control as a whole. When other controls are added to the control — for example, labels or text boxes in list items — they are in turn bounded to the container, namely the DataList control.