Editing DataList Items
The DataList control supports in-place editing of the data in an item through its EditItemTemplate
property. The EditItemTemplate defines the content and appearance of the item when it is being edited. For
example, the following template includes a text box, an "Update" button and a "Cancel" button.
<EditItemTemplate>
Item: <asp:TextBox id="Text1" runat="server"
Text='<%# ((DataRowView)Container.DataItem)["Item"] %>'
/><br>
<asp:LinkButton id="button1" runat="server"
Text="Update"
CommandName="update"
/>
<asp:LinkButton id="button2" runat="server"
Text="Cancel"
CommandName="cancel"
/>
</EditItemTemplate>
The EditItemTemplate interacts with another property: EditItemIndex. By default, the value of
EditItemIndex is -1, meaning none of the items in the list is being edited. When EditItemIndex is set to a
particular item, that item is displayed using the EditItemTemplate.
The DataList also supplies three events that can be used to support editing. EditCommand is thrown
when an "edit" command button control is clicked within the list's ItemTemplate. It's up to you to handle this event
in your code. The typical logic sets EditItemIndex to the selected item, and then rebinds the data to the DataList as shown in the following example.
Protected Sub DataList_EditCommand(Source As Object, e As DataListCommandEventArgs)
DataList1.EditItemIndex = CType(e.Item.ItemIndex, Integer)
BindList()
End Sub
VB
The EditItemTemplate typically contains "update" and "cancel" command buttons. These buttons cause the
UpdateCommand and CancelCommand events to be thrown, respectively. It's up to you to handle these events
in your code. The typical logic for "cancel" sets EditItemIndex to -1, and then rebinds the data to the DataList as shown in the following example.
Protected Sub DataList_CancelCommand(Source As Object, e As DataListCommandEventArgs)
DataList1.EditItemIndex = -1
BindList()
End Sub
VB
The typical logic for "update" updates the data source, sets EditItemIndex to -1, and then rebinds the data to the
DataList. The following sample illustrates editing items in DataList.
Copyright 2001 Microsoft Corporation. All rights reserved.