In this article, I will discuss some More Features of DataList in ASP.NET. Earlier, I have explained important properties of DataList such as templates, styles, and properties related to data keys that you can find here.
Read-Only Fields in DataList Control
In fact, the DataList control contains five read-only fields that we use in various event handler functions. For instance, the EditCommandName field represents the name of the Edit command that we use when we need to implement Edit/Update/Cancel functionality. Besides EditCommandName, there are two more fields that we use along with it. Basically, these fields are UpdateCommandName and CancelCommandName that represent the name of the Update command and Cancel command respectively. Similarly, the DeleteCommandName represents the name of the Delete command. Also, there is a SelectCommandName field that represents the name of the Select command.
Important Methods of the DataList Control
Although DataList control has numerous methods, the following methods we use frequently to perform database operations.
Basically, the CreateItem(Int32, ListItemType) method creates an object of type DataListItem that you can use to create a custom implementation of the DataList control. Likewise, the DataBind() method binds the DataList control to a data source such as an object of DataTable or DataSet.
In case we need to find out a container within the DataList control itself. For example, suppose we compute a value such as summation of the values of a field and we want to show it in a Label control in FooterTemplate. In such a case, the FindControl(String) method searches for the particular control with its Id value passed as a String parameter.
The following are some of the important events that DataList control handles.
Whenever the DataList control binds to a data source, the DataBinding event occurs. As soon as an item in the DataList control is created, the ItemCreated event occurs. Hence, we can use the corresponding event handler method to fetch a particular value for that item.
The ItemDataBound event occurs whenever an item is data-bound. The difference between these two events is that the ItemCreated event occurs before any data is there in the cell whereas ItemDataBound occurs whenever an item is data-bound. Particularly, the ItemCreated event also occurs on postbacks.
In contrast, the ItemCommand event is raised whenever a Button within the DataList control is clicked. Besides the ItemCommand event, the other events related to the click of a specific command button are EditCommand, UpdateCommand, DeleteCommand, and CancelCommand. Moreover, the SelectedIndexChanged event occurs when a user selects an item in the list.
Now that, I have discussed the important features of the DataList control, I will demonstrate it using a simple example.
A Simple Example of Using a DataList Control.
In this article on More Features of DataList, the important features of DataList control are discussed. Particularly, the read-only fields, events, and methods of DataList control are discussed here. Without delay, let us go through an example of DataList control.