Controls You Can Use on Web Forms ASP.NET Data Controls DataList Control
The DataList control provides several events that you can program against. This allows you to run a custom routine whenever an event occurs. The following table lists the events supported by the DataList control.
Event |
Description |
CancelCommand |
Occurs when the Cancel button is clicked for an item in the DataList control. |
DeleteCommand |
Occurs when the Delete button is clicked for an item in the DataList control. |
EditCommand |
Occurs when the Edit button is clicked for an item in the DataList control. |
ItemCommand |
Occurs when any button within a DataList control is clicked. This event is often used to perform a task when a button is clicked in the control. |
ItemCreated |
Occurs after all DataListItem objects are created in the DataList control. This event is often used to modify the values of a record before it is displayed. |
UpdateCommand |
Occurs when the Update button is clicked for an item in the DataList control. |
If a DataList control template includes a Button, LinkButton, or ImageButton Web server control, these buttons include a click event that can bubble up to the containing DataList control. This allows you to define your own functionality for the DataList control.
NOTE: Only controls that support a Command event can bubble their events to the DataList control.
The following example illustrates a way to handle the ItemCommand event of a DataList. Information about the selected item is displayed whenever any of the LinkButton controls within the DataList is clicked.
The below snippet shows how to attach a handler for the event.
<asp:datalist id = "myDataList" runat = "server" onItemCommand = "showItem" ... >
The below shows how the handler method is defined.
void showItem ( Object src, DataListCommandEventArgs e ) {
myDataList.SelectedIndex = e.Item.ItemIndex;
string query = "SELECT * FROM Employees";
myDataList.DataSource = fetchData ( query, "northwind" );
myDataList.DataBind ( );
}
Show me
The following example illustrates a way to handle the ItemCreated event of a DataList. In this case, the sample simply retrieves and displays either the item type or data values of each DataListItem, in the order in which the items are created.
The below snippet shows how to attach a handler for the event.
<asp:datalist id = "myDataList" runat = "server" onItemCreated = "showItem" ... >
The below shows how the handler method is defined.
void showItem ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
object [ ] dataitems = ( ( DataRowView ) e.Item.DataItem ).Row.ItemArray;
items.Text += "<br>" + dataitems [ 0 ] + " " + dataitems [ 1 ] + ", " + dataitems [ 2 ];
}
else {
items.Text += "<br>" + e.Item.ItemType.ToString ( );
}
}
Show me
The following example illustrates a way to handle the ItemDataBound event of a DataList. The page contains a DataList control that is used to display a list of hotels based on user input.
In this case, the ItemDataBound event handler is used to accomplish the following:
- obtain and set the ImageUrl, NavigateUrl, ToolTip and Attributes properties for the hotel image hyperlink;
- obtain and set the Text, NavigateUrl, ToolTip and Attributes properties for the hotel name hyperlink;
- obtain and set the ImageUrl, NavigateUrl, ToolTip and Attributes properties for the rating image hyperlink;
- compute and set the average nightly rate.
The below snippet shows how to attach a handler for the event.
<asp:datalist id = "myDataList" runat = "server" onItemDataBound = "showItem" ... >
The below shows how the handler method is defined.
void setListBindings ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
... see example ...
}
}
Show me
Introduction to the DataList Control Adding DataList Controls to a Web Forms Page Web Forms Events and Handlers