The DataList Web server control displays rows of database information in customized format. These two controls play an important role in ASP.NET. They represent many common features.
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 DataGrid Web server control is a multi-column, data-bound grid. Columns can be made that displays and edit data. Multi-columns include Edit, Update, Cancel, Select buttons, Custom Buttons, and Template Columns. Therefore Template Columns can further be laid in Template-Editing Mode.
Controls performing support function
Before discussing these controls thoroughly, it is important to know how these controls support event bubbling, templates, and the DataKeys collection.
Event Bubbling
There are three standard controls in ASP.NET that support event bubbling:
- the Repeater,
- DataList, and
- DataGrid controls.
These controls capture events in their child controls. When an event is raised in a child control, it "bubbles up" in the containing control, which can then execute a subroutine to handle the event.
It is seen that in Databinding section, a list of LinkButton controls can be displayed in a Repeater control. When a LinkButton control is clicked, a Click event is raised.Therefore instead of writing code to handle the Click event for each LinkButton individually, one subroutine with any Click event can be associated , that is raised in the Repeater control.
The Repeater, DataList, and DataGrid controls enable an association of a subroutine with an event bubbled up from a child control by specifying a value for the OnltemCommand property.
For example, in case of the Repeater control, a subroutine would be associated with the ItemCommand event like this:
| Example: |
<asp:Repeater
OnItemCommand="RepeaterItemCommand"
Runat="Server".>
<ItemTemplate>
<li>
<asp:LinkButton
Text='<%# Container.DataItem( "FirstName")%>’
Runat="Server" />
</ItemTemplate>
</asp:Repeater> |
| |
When any of the LinkButton controls rendered by the Repeater control are clicked the Click event occurs, the RepeaterItemCommand subroutine is executed to handle the event.
The DataList and DataGrid controls go a step further by allowing distinctions to be made between the events raised by their child controls.
The DataList and DataGrid controls support the following five events:
• ItemCommand
• UpdateCommand
• EditCommand
• DeleteCommand
• CancelCommand
When a child control is created in a DataList or DataGrid, the type of event to be raised in the containing control can be indicated by using the CommandName property.
For example, if you want to raise an UpdateCommand event, you would specify the CommandName property like this:
| Example: |
<asp:LinkButton
Text="Update Field!"
CommandName="update"
Runat="Server" /> |
| |
Three controls of ASP.NET have CommandName property as LinkButton, Button, and ImageButton. They also have a CommandArgument property that enables to specify an optional argument that is passed when the control is clicked. So, an optional argument can be bubbled up by using an event like this:
| Example: |
<asp:LinkButton
Text=”Update Field!”
CommandName="update"
CommandArgument="12"
Runat="Server" /> |
| |
Using Templates
Templates have been discussed earlier. The Repeater control supports several templates that enables to format the output of the control.
For example, you can use the ItemTemplate to format how the Repeater control renders each item that it displays.
The DataList and DataGrid controls also support templates. The DataList control supports the following templates:
| Template |
Description |
| ItemTemplate |
Is an HTML elements and controls, which is rendered once for each row in the data source |
| AlternatingItemTemplate |
It is like the ItemTemplate element, but it is rendered for every other row in the DataList control. If this template is used a typically different look is created for it, such as a different background color than that of the ItemTemplate. |
| SelectedItemTemplate |
Is an element to render when the user selects an item in the DataList control. Typically it is used to visually mark the row using a background or font color. Displaying additional fields from the data source can expand the item |
| EditItemTemplate |
Is the layout of an item when it is in edits mode. This template typically contains editing controls, such as TextBox controls. |
| HeaderTemplate and FooterTemplate |
The text and controls are used to render at the beginning and end of the list. |
| SeparatorTemplate |
Is used to render between each item. A typical example might be a line (using an <HR> element). |
|