To specify the appearance of items in a template, the style of Template’s can be set. For example, you might specify that:
- Items are rendered with black text on a white background
- Alternating items are rendered with black text on a light gray background
- The selected item is rendered with bold black text on a yellow background
- Items being edited are rendered with black text on a light blue background
Each template supports its own style object whose properties can be set both at the time of design and at the time of the run.
When an item is selected in a DataList, the SelectedItem template is displayed, and when it is selected for editing in a DataList, the EditItem template is displayed.
The DataGrid control, supports the following four templates when a column is displayed using a TemplateColumn:
Controls how the header of the DataGrid is formatted
Controls the formatting of items displayed by the DataGrid
Controls how the footer of the DataGrid is formatted
Controls how an item selected for editing is formatted
The Edit ItemTemplate is displayed when you select an item for editing in a DataGrid.
Using the Datakeys Collection
DataKeys collection is used to access the key values of each record (displayed as a row) in a data listing control. This allows storing the key field with a data listing control without displaying it in the control. This collection is automatically filled with the values from the field specified by the DataKeyField property.
The key field is commonly used in a handler for an event, such as ItemCommand or DeleteCommand, as part of an update query string, to revise a specific record in the data source. The key field helps to update the query string identifying the appropriate record to modify.
Both the DataList and DataGrid controls have a special collection named DataKeys. This collection represents the primary key field associated with each item in the control.
For example, imagine that you have a database table named Employees and the Employees table further has an identity column named EmployeeID which you want to display and edit with a DataGrid control.
You can associate the value of the identity column with each item in the DataGrid by assigning the name of the identity column to the DataKeyField property of the DataGrid like this:
The advantage of associating the identity column with each item in the DataGrid is that the value of the column can be retrieved whenever a field is edited in the DataGrid.
If a field in the DataGrid is updated and further requires the corresponding field to be updated in the underlying database table, the DataKeys collection can be used to uniquely identify the correct row in the database table for the update.
The DataList Web server control displays rows of database information in customized format. The displayed data in customized format is defined in the item, alternating item, selected item, and edit item templates. Header, footer, and separator templates are also available to customize the overall appearance of the DataList. By including Button Web server controls in the template, list items can be connected to the code that allows users to switch between display, selection, and editing modes.
The following sections, will show the use of DataList control, to format and display data from a database table, to create multicolumn menus and to use the DataList to edit data in a database table.
Displaying Data In a DataList
A DataList can be used like a Repeater control-to display records from a database table. However, unlike a Repeater control, a default behavior of DataList controls default is to show the database records within an HTML table.
Look at the following example: -
The output of above example is show below: -
In the above example each row from the Employees table is displayed in a separate HTML table row ,when the DataList displays the records.