DataList Control for ASP. NET data binding, asp. netdatalist

Source: Internet
Author: User

DataList Control for ASP. NET data binding, asp. netdatalist

The DataList control is a control in. NET. The DataList control presents data in the form of a table (you can edit it in the property generator). With this control, you can use different la s to display data records (using template editing), for example, sorts data records into columns or rows. You can configure the DataList control so that you can edit or delete records in the table (using the EditItemTemplate template and SelectedItemTemplate template ). The DataList control does not use the data modification function 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. The selection and editing functions are supported.
  • 2. DataList supports visual template editing and attribute editing, while the Repeater control does not specify the built-in layout. It is more difficult to edit data than DataList.
  • 3. The content in DataList is presented as a table, which makes data arrangement more beautiful, and Repeater needs to add a table.
  • 4. DataList explicitly places the item in the HTML table, but Repeater does not.

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

Iii. Events

1. Bubble events
The ". NET" Framework contains three standard controls that support event bubbling: Repeater, DataList, and DataGrid controls. These controls allow you to capture events of its child controls. When a sub-control generates an event, the event is "Bubbling" to the container control that contains the sub-control, and the container control can execute a sub-program to process the event.
The DataList control supports event bubbling. It can capture events generated by controls contained in DataList and process these events through common subprograms. At this point, some people may not understand the benefits of event bubbling. In this way, we will reverse our thinking: If there is no event bubbling, therefore, a corresponding processing function is required for the events generated by each control contained in DataList. What if DataList contains 10000 controls? Or more? How many event handlers do we have to write. So with event bubbling, no matter how many controls are contained in DataList, we only need one handler. My understanding is to encapsulate the program and then reject the problem through the inheritance mechanism.

2. events supported by DataList
EditCommand: generated by a subcontrol with CommandName = "edit.
CancelCommand: generated by a subcontrol with CommandName = "cancel.
UpdateCommand: generated by a subcontrol with CommandName = "update.
DeleteCommand: A subcontrol with CommandName = "delete" is generated.
ItemCommand: the default event of DataList.

3. event triggering Process

With these five events, which event should I trigger when I click a button in the DataList control? When will they be triggered?
In ASP point NET, three controls have the CommandName attribute, namely, Button, LinkButton, and ImageButton. You can set their CommandName attribute to indicate the time type generated in the container control. For example, if the CommandName attribute of a LinkButton in DataList is set to "update", clicking this button triggers the UpdateCommand event of DataList, 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 a button with the CommandName delete/cancel/update/edit in any DataList control is clicked, The ItemCommand event is triggered first, and then the corresponding event.

4. edit data in DataList

1. Select the primary key of a DataList item and use the DataKeys set in the DataList control.
When selecting an item in DataList, you usually need to obtain the value of the primary key associated with this item. You can use the DataKeys set to obtain the value of the primary key associated with an item. After creating a DataKeys set, you can pass the index value of the item to the DataKeys set to obtain the primary key value associated with the related item in DataList. For example, to obtain the primary key value of the third item displayed by DataList, you can use DataList1.DataKeys [2]. use DataList1.DataKeys [e. item. itemIndex].

2. Edit the items in DataList
You can use the DataList control to edit a record in a data table. In fact, it is very convenient to complete such operations in DataList, unlike the switch between multiple pages in asp. The DataList control has a template named EditItemTemplate. You can place a Form Control in EditItemTemplate to edit specific items in DataList. When the value of the EditItemIndex attribute of DataList is an index of DataList, the corresponding items are displayed in the EditItemTemplate template. When the value of the attribute is-1, the EditItemTemplate template is not displayed.

3. select items in DataList
After the data is bound to DataList, each item in DataList has an index number. The index of the first item is 0, which is sequentially numbered. We can use indexes to determine specific items in DataList.
DataList displays data items in ItemTemplate, ItemTemplate, and AlternatingItemTemplate by default. When the value of the SelectedIndex attribute of DataList is an index of DataList, the corresponding items are displayed in the SelectedItemTemplate template. If the property value is-1, the SelectedItemTemplate template is not displayed.

After talking about this, it is just a matter of mouth. The above are some theoretical knowledge. Only when I use it can I deeply understand the functions. The next article is a practical article on DataList. Stay tuned!

Articles you may be interested in:
  • Determine when to use DataGrid, DataList, or Repeater (ASP. NET technical article)
  • Asp.net GridView and DataList to move the mouse over the row to change color
  • The asp.net Datalist control implements the paging function.
  • Asp.net datalist usage
  • Asp.net uses the for loop to display Datalist Columns
  • Differences between asp.net DataList and Repeater
  • Asp.net uses aspnetpager as the DataList page
  • Asp.net Summary of getting Checkbox values in Datalist
  • Implementation Method and instance code for binding a database to the DataList control in asp.net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.