The DataList control displays database information in a format that you can control very precisely using templates and styles. The DataList control is useful for displaying rows of database information. Optionally, you can configure the DataList control to allow users to edit or delete information. You can also customize the control to support add other functionality such as selecting rows.
Note To display read-only database information, you can also use the Repeater Web control. If youe want to include paging and sorting automatically, you can use the DataGrid Web control.
The following sections introduce you to the features of the DataList control.
The DataList control must be bound to a data source. If the control is not bound, the control will not be rendered on the page. The DataList control can use as a data source any class that supports the IEnumerable interface. In practical terms, this means that you can use any type of data source that you can create using the Visual Studio data designers. You can also use data sources that you can add to your page from the Toolbox, such as the SQLDatSetCommand and ADODataSetCommand classes. You can also bind to simpler structures, such as an array of type ArrayList.
When data binding, you specify a data source for the DataList control as a whole. You can then bind individual controls within the list — for example, labels or text boxes in list items — to the DataList control as the container.
For more information, see Web Forms Data Binding.
In the DataList control, you define the layout of your information using templates. The control supports the following templates:
As a minimum, you must define an ItemTemplate layout. To support editing, you would add an EditItemTemplate. Other templates are optional.
For more information, see Web Forms Controls Templates.
To specify the appearance of items in a template, you can set its styles. For example, you might specify that:
Each template supports its own style object whose properties you can set both at design time and at run time.
For more information, see Web Forms Controls and CSS Styles.
Not only can you use templates to specify the layout of controls and text within individual items, but you can specify how the items are laid out with respect to one another. The DataList control offers you these options:
In table layout, the items are rendered into an HTML table, which gives you more facilities for specifying the look of items, because it allows you to set properties for table cells, such as gridlines.
The DataList control supports six events. One of them, the OnItemCreated event, gives you a way to customize the item-creation process. For example, you might conditionally set style object properties for new items as they are created.
The remaining events are raised in response to button clicks in list items. They are designed to help you respond to the most commonly used functionality for the DataList control: editing items, deleting them, updating the edited items, and canceling edits.
When a button is clicked in an item, its event is bubbled to the container — the DataList control. The exact event it raises depends on the Command property of the button that was clicked. For example, if the Command property of a button is edit, the button causes an OnEditCommand event to be raised. If the Command property is delete, the button causes an OnDeleteCommand event to be raised, and so on.
The DataList control also supports a generic OnItemCommand event that is raised when any other button click occurs in an item. You can use this event for custom functionality by setting a button's Command property to a value you need and then testing for it in the OnItemCommand event-handling method. (For example, selecting an item, as documented in Allowing Users to Select Items.)
For more information, see Web Forms Event Model.
You can allow users to edit individual items in the control. The general strategy is to create an EditItemTemplate that provides the appropriate layout and controls for editing. You must also provide a way for users to indicate that they want to edit the item. The most common way is to include a button in the item template (and AlternatingItemTemplate, if you are using that) and set the button's Command argument to edit. Then when the button is clicked, the DataList control automatically raises the OnEditCommand event. In the event-handling method for that event, you set the item to edit mode, which displays the EditItemTemplate.
The EditItemTemplate usually includes buttons to allow the user to save the changes or discard them (for example, Update and Cancel buttons). These buttons work similarly to the edit button — they send a predefined command message (update and cancel) to the DataList control, which raises OnUpdateCommand and OnCancelCommand events that you can respond to appropriately.
The process for selecting an item is similar, with one exception: there is no specific event corresponding to a selection. You still create a button and set its Command property to something like select. However, because there is no selection event, you write an event-handling method for the generic OnItemCommand event. In that event-handling method, you must test the command, and if it is select, put the item into selection mode.