Detailed explanation of 'ASP.NET' data binding—DataList

黄舟
Release: 2017-03-08 13:10:54
Original
1951 people have browsed it

The DataList control is a control in .NET. The DataList control presents data in the form of a table(editable in the property builder). Through this control, you can use different layouts to display data records(using templates Edit), for example, arrange data records into columns or rows. You can The DataList control is configured to enable users to edit or delete records in the table (using the EditItemTemplate template and the SelectedItemTemplate template) . The DataList control does not use the data modification capabilities of the data source control; you must provide this code yourself.

1. Comparison between DataList and Repeater

1. DataList has two more templates than Repeater: SelectedItemTemplate and EditItemTemplate , supports selection and editing functions.

2. DataList has visual template editing and attribute editing, while the Repeater control does not specify a built-in layout. Compared with DataList, data editing is more troublesome.

3. The content in the DataList presents data in the form of tables. This makes the data arrangement more beautiful, and the Repeater itself needs to add tables.

4. DataList explicitly places items in the HTML table, but Repeater does not.

2. Templates in DataList

1. ItemTemplate, AlternationgItemTemplate, SeparatorTemplate, HeaderTemplate, FooterTemplate, SelectedItemTemplate, EditItemTemplate.

3. Event

1. Bubble event

The ".NET" framework contains three standard controls that support event bubbling: Repeater, DataList and DataGrid controls. These controls allow you to capture events from 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.

The DataList control supports event bubbling, which can capture events generated by controls contained in the DataList and handle these events through ordinary subroutines. At this point, some people may not understand the benefits of event bubbling. In this way, we think about it in reverse: If there is no event bubbling, then a corresponding processing function needs to be defined for each event generated by each control contained in the DataList. What if the DataList contains 10,000 controls? Or more? So how many event handlers do we have to write. So with event bubbling, no matter how many controls are contained in the DataList, we only need one handler. My understanding is to encapsulate the program and then solve the problem through the inheritance mechanism.

2. Events supported by DataList:

EditCommand: Generated by a child control with CommandName="edit".

CancelCommand: Generated by a child control with CommandName="cancel".
UpdateCommand: Generated by a child control with CommandName="update".
DeleteCommand: generated by a child control with CommandName="delete".
ItemCommand: Default event of DataList.


## 3. Event triggering process: With these five events, when I click on the DataList control When a certain button is pressed, which event should be triggered? When are they triggered?

There are three controls with CommandName properties in "ASP Point NET", namely Button, LinkButton and ImageButton. Their CommandName properties can be set to represent the time type generated in the container control. For example, if you set the CommandName property of a LinkButton in the DataList to "update", then when this button is clicked, the UpdateCommand event of the DataList will be triggered. 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. After any button with the CommandName of delete/cancel/update/edit in the DataList control is clicked, the event ItemCommand is triggered first, then the corresponding event.

4. Edit the data in the DataList.

1. Edit by selecting the primary key of an item in the DataList, use the DataList The DataKeys collection in the control.

When selecting an item in the DataList, you usually need to obtain the value of the primary key associated with the item. You can use the DataKeys collection to obtain the value of the primary key associated with an item. After creating the DataKeys collection, you can obtain the primary key value associated with the relevant item in the DataList by passing the index value of the item to the DataKeys collection. For example, to get the primary key value of the third item displayed by DataList, you can use: DataList1.DataKeys[2]. If you want to get the primary key value of the item where an event occurs in the event handler of the DataList control, use: DataList1.DataKeys[e.Item.ItemIndex].

2. Edit the items in the DataList

## You can use the DataList control To edit a record in the data table, in fact, it is very convenient to complete such an operation in DataList, unlike in asp where you need to switch back and forth between multiple pages. The DataList control has a template named EditItemTemplate, in which form controls are placed so that specific items can be edited in the DataList. When the value of the EditItemIndex property of the DataList is the index of an item in the DataList, the corresponding item will be displayed in the EditItemTemplate template; When the property value is -1, it means that the EditItemTemplate template will not be displayed.

## 3. Select the item # in the DataList ## After the data is bound to the DataList, each item in the DataList has an index number. The index of the first item is 0, and the numbers are numbered downwards. We can use indexes to determine specific items in the DataList.

DataList displays data items in the ItemTemplate or ItemTemplate+AlternatingItemTemplate template by default. When the value of the SelectedIndex property of the DataList is the index of an item in the DataList, the corresponding item will be displayed in the SelectedItemTemplate template. When the value of this attribute is -1, it means that the SelectedItemTemplate template is not displayed.


4. Summary

Having said so much, it is just talk without practice. The above are all theoretical knowledge. , only when you use it yourself can you deeply understand its functions. The next blog will be a practical chapter on DataList, so stay tuned! ! !

The above is the detailed content of Detailed explanation of 'ASP.NET' data binding—DataList. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template