Detailed "ASP." Data binding-datalist

Source: Internet
Author: User
The DataList control is a control in. Net. The DataList control renders data as a table (editable in the property builder), which allows you to display data records using different layouts (editing with templates), for example, to sort data records into columns or rows. You can configure the DataList control to enable users to edit or delete records in a table (using EditItemTemplate templates and SelectedItemTemplate templates). The DataList control does not use data modification features of the data source control, and you must provide this code yourself.

A comparison of DataList and repeater

1, DataList more than repeater two templates: SelectedItemTemplate and EditItemTemplate, support the selection and editing functions.

2, DataList has visual template editing and property editing, and repeater control does not specify a built-in layout, compared with DataList, data editing is more troublesome.

3, DataList in the form of data presented in tabular format, so that the data arrangement more beautiful, and repeater himself need to add a table.

4, DataList the item explicitly placed in the HTML table, repeater is not.

Second, the template in the DataList

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

Iii. 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 produces an event, the event "bubbles" to the container control that contains the child control, and the container control can execute a subroutine to handle the event.

The DataList control supports event bubbling, captures events generated by controls contained within DataList, and handles these events through normal subroutines. Here are some people who may not quite understand the benefits of event bubbling, so we think in turn: if there is no event bubbling, then the event generated for each control contained within DataList needs to define a corresponding handler, 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 included in the DataList, we just need a handler to do it. my understanding is to encapsulate the program and then use the inheritance mechanism to deny the problem.

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".
The default event for Itemcommand:datalist.


3. Event-Triggered process: With these five events, which event should I trigger when I click on one of the buttons in the DataList control? When will they be triggered?

In ASP point NET, there are three controls with the CommandName property, button, LinkButton, and ImageButton, which can set their CommandName properties to represent the type of time produced within the container control. For example, if you set the CommandName property of a LinkButton in DataList to "Update", then clicking this button will trigger 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 that is generated by the DataList control, and any DataList control in the CommandName for Delete/cancel/update/edit button is clicked after the The event ItemCommand is first triggered and then the corresponding event.

Iv. edit the data in the DataList.

1. Edit by selecting the primary key for an item in DataList, using the DataKeys collection in the DataList control.

When you select an item in DataList, you typically need to get the value of the primary key associated with the item. You can use the DataKeys collection to get the value of the primary key that you want to associate with an item. After you have created the DataKeys collection, you can get the primary key value associated with the related item in 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 have the primary key value of the item for the event in the event handler of the DataList control, 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 do this in DataList, unlike the need to switch back and forth across multiple pages in ASP. The DataList control has a template named EditItemTemplate that places the form controls in EditItemTemplate so that specific items can be edited in DataList. When the value of the EditItemIndex property of DataList is DataList an index of an item, the corresponding item is displayed in the EditItemTemplate template, and when the property value is-1, the EditItemTemplate template is not displayed.

3. Select an item in DataList

After data is bound to DataList, each item in the DataList has an index number, the first item has an index of 0, and is numbered sequentially. We can use the index to determine the specific items in the DataList.
DataList default to display data items in ItemTemplate or itemtemplate+alternatingitemtemplate templates, When the value of the SelectedIndex property of DataList is DataList an index of an item, the corresponding item is displayed in the SelectedItemTemplate template. When the property value is-1, the SelectedItemTemplate template is not displayed.

Iv. Summary

said so much, light said not practice is mouth bashi, above are some theoretical knowledge, only their own to be able to deeply appreciate the function, the next blog is aimed at DataList practice, please pay attention!!!


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.