Asp. NET Data Binding DataList control _ Practical Tips

Source: Internet
Author: User
Tags dot net

The DataList control is a control in. Net. The DataList control renders data as a table (which you can edit in the Property builder), where you can use a different layout to display data records (edited using templates), such as arranging 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 the data modification feature of the data source control, you must provide this code yourself.

A comparison of DataList and repeater

    • 1, DataList than repeater two more templates: SelectedItemTemplate and EditItemTemplate, support the selection and editing functions.
    • 2, DataList has visual template editing and property editing, and the Repeater control does not specify a built-in layout, compared with DataList, data editing more cumbersome.
    • 3, DataList in the form of the contents of the table to render the data, so that the data arrangement more beautiful, and repeater need to add a table to do.
    • 4, DataList the item explicitly in the HTML table, repeater is not.

Second, the template in the DataList
ItemTemplate, Alternationgitemtemplate, SeparatorTemplate, HeaderTemplate, FooterTemplate, SelectedItemTemplate, EditItemTemplate.

III. Events

1. Bubbling events
the. NET framework contains three standard controls that support event bubbling: The Repeater, DataList, and DataGrid controls. These controls allow you to capture events for their child controls. When a child control produces an event, the event is "bubbling" 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, capturing events generated by controls contained within the DataList, and handling these events through normal subroutines. There are some people here who may not understand the benefits of event bubbling, so we think in reverse: if there is no event bubbling, then for each control contained within the DataList, you need to define a corresponding handler function 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. My understanding is to encapsulate the program and then reject the problem through the mechanism of inheritance.

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, the event triggers the process

With these five events, which event should I trigger when I click on a button in the DataList control? When will they be triggered?
There are three controls in ASP dot net with CommandName properties, button, LinkButton, and ImageButton, which can set their CommandName properties to represent the type of time that is generated within the container control. For example, if you set the CommandName property of a LinkButton in DataList to "Update", then when you click this button, the DataList UpdateCommand event will be triggered. 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 when the button commandname for Delete/cancel/update/edit in any DataList control is clicked, 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 of 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 a primary key that you want to associate with an item. After the DataKeys collection has been created, you can obtain the primary key value associated with the related 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], and if the primary key value of the item that occurs in the event handler function of the DataList control is to be used: datalist1.datakeys[ E.item.itemindex].

2, edit the items in the DataList
You can use the DataList control to edit a record in a datasheet, in fact, it is very convenient to do this in DataList, unlike the need to switch back and forth across multiple pages in the 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 a DataList EditItemIndex property is the index of a DataList item, the corresponding item will be displayed in the EditItemTemplate template, and the EditItemTemplate template is not displayed when the property value is-1.

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

Said so much, light said not practice is the mouth, above are some theoretical knowledge, only to use the ability to deeply understand the function, the next one is for 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.