ASP. DataList Control for NET Data Binding

  • 2021-07-09 07:57:40
  • OfStack

The DataList control is a control in. NET. The DataList control, which renders data as a table (editable in the Property Builder), allows you to display data records in different layouts (edited using templates), such as arranging data records in columns or rows. You can configure DataList controls to enable users to edit or delete records in tables (using EditItemTemplate templates and SelectedItemTemplate templates). The DataList control does not use the data modification capabilities of the data source control, and you must provide this code yourself.

1. Comparison between DataList and Repeater

1. DataList has two more templates than Repeater: SelectedItemTemplate and EditItemTemplate, which support selection and editing functions. 2. DataList has visual template editing and attribute editing, while Repeater control does not specify built-in layout. Compared with DataList, data editing is troublesome. 3. The content in DataList presents data in the form of tables, which makes the data arrangement more beautiful, while Repeater needs to add tables. 4. DataList explicitly places items in the HTML table, while Repeater does not.

2. Templates in DataList
ItemTemplate, AlternationgItemTemplate, SeparatorTemplate, HeaderTemplate, FooterTemplate, SelectedItemTemplate, EditItemTemplate.

3. Events

1. Bubbling event
The ". NET" framework contains three standard controls that support event bubbling: Repeater, DataList, and DataGrid controls. These controls allow you to capture the events of their child controls. When a child control generates an event, the event "bubbles" to the container control containing the child control, and the container control can execute a subroutine to handle the event.
DataList controls support event bubbling, which captures events generated by controls contained in DataList and handles these events through ordinary subroutines. At this point, some people may not quite understand the benefits of event bubbling, so we think in reverse: If there is no event bubbling, then a corresponding handler needs to be defined for every event generated by every control contained in DataList. What if DataList contains 10,000 controls? Or more? How many event handlers do we have to write? So with event bubbling, no matter how many controls are included in DataList, we only need one handler. My understanding is to encapsulate the program and then resolve the problem through the inheritance mechanism.

2. Events supported by DataList
EditCommand: Produced by a child control with CommandName= "edit".
CancelCommand: Produced by a child control with CommandName= "cancel".
UpdateCommand: Produced by a child control with CommandName= "update".
DeleteCommand: Produced by a child control with CommandName= "delete".
ItemCommand: The default event for DataList.

3. The process of event triggering

With these 5 events, which event should be triggered when I click a button in the DataList control? When will they trigger?
In "ASP Point NET", there are three controls with CommandName properties, namely Button, LinkButton and ImageButton, and their CommandName properties can be set to indicate the type of time generated within the container control. For example, if the CommandName attribute of one LinkButton in DataList is set to "update", when this button is clicked, the UpdateCommand event of DataList will be triggered, and we can write the relevant processing code to the corresponding event handler.
Note: The ItemCommand event is the default event generated by the DataList control. When the button for CommandName to be delete/cancel/update/edit in any DataList control is clicked, the event ItemCommand is triggered first and then the corresponding event.

4. Edit data in DataList

1. Edit by selecting the primary key of an item in DataList, using the DataKeys collection in the DataList control.
When you select an item in DataList, you usually need to get the value of the primary key associated with that item. You can use the DataKeys collection to get the value of the primary key that you want to associate with 1 item. After the DataKeys collection is created, the primary key values associated with the related items in the DataList can be obtained by passing the index values of the items to the DataKeys collection. For example, to get the primary key value of item 3 displayed by DataList, you can use: DataList1.DataKeys [2] or: DataList1.DataKeys [e. Item. ItemIndex] if the primary key value of the item for which the event occurs in the event handler of the DataList control.

2. Edit items in DataList
You can use the DataList control to edit a record in a data table. In fact, it is very convenient to do this in DataList, unlike switching back and forth between multiple pages in asp. The DataList control has a template called EditItemTemplate, where form controls are placed in EditItemTemplate so that specific items can be edited in DataList. When the value of EditItemIndex attribute of DataList is the index of one item of DataList, the corresponding item will be displayed as EditItemTemplate template; When the attribute value is-1, it means that the EditItemTemplate template is not displayed.

3. Select the item in DataList
When data is bound to DataList, every entry in DataList has an index number, and the index of the first entry is 0, which is numbered down in turn. We can use indexes to determine specific items in DataList.
By default, DataList displays data items in ItemTemplate or ItemTemplate+AlternatingItemTemplate template. When the value of SelectedIndex attribute of DataList is the index of one item of DataList, the corresponding item will be displayed in SelectedItemTemplate template. When the attribute value is-1, it means that the SelectedItemTemplate template is not displayed.

Having said so much, it's a mouth-to-mouth style to talk but not practice. All of them are theoretical knowledge. Only when you use them can you deeply understand their functions. The next one is a practical one for DataList, so please pay attention!


Related articles: