NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Allowing Users to Edit Items

You can specify that users can edit individual items in the DataList Web control. When an individual item is put into edit mode, its editable values are usually displayed in text boxes or other controls that the user can change values in.

To allow users to select items in a DataList control

  1. Create an EditItemTemplate for the control to include appropriate text and controls, and set the template's styles. For details, see Creating Web Control Templates and Customizing Control Appearance Using Styles.
  2. In the ItemTemplate (and AlternatingItemTemplate, if you are using that), add a Button or LinkButton Web control and set its Text property to "Edit" or something similar. Set the Command property for this button to edit (case-sensitive).
  3. Create an EditItemTemplate for the DataList control that includes the following:
    • Controls for all the values that the user can change. For example, include TextBox controls for all character and numeric data.
    • A Button or LinkButton control with the text "Update" whose Command property is set to update (case-sensitive).
    • A Button or LinkButton control with the text "Cancel" whose Command property is set to cancel (case-sensitive).

    The Update button will allow users to specify that they are finished editing. The Cancel button will allow them to quit editing without saving changes.

  4. Create an event-handling method for the control's OnEditCommand event. In the event-handling method, do the following:
    • Set the DataList control's EditItemIndex property to the index value of the item to put into edit mode. The index of the item the user clicked is available via the Item object's ItemIndex property.
    • Bind the control to its data source.

    The event-handling method might look like this:

    [C#]
    protected void DataList1_OnEditCommand(object Source, 
       DataListCommandEventArgs e)
    {
       DataList1.EditItemIndex = (int)e.Item.ItemIndex;
       DataList1.DataBind();
    }
  5. Create a method for the OnUpdateCommand event that reads the values of the editing controls in the current item and writes them back to the data source. To get the value of a specific control in the item, use the FindControl method of the Item event-argument object. After updating the data source, switch the item back out of editing mode by setting the EditItemIndex property to -1, and then rebind to the data source:
    [C#]
    protected void DataList1_OnUpdateCommand(object Source, 
       DataListCommandEventArgs e)
    {
       string name = ((TextBox)e.Item.FindControl("txtName")).Text;
       string qty = ((TextBox)e.Item.FindControl("txtQty")).Text;
       string price = ((TextBox)e.Item.FindControl("txtPrice")).Text;
    
       // Update data source here
    
       DataList1.EditItemIndex = -1; // Switch out of edit mode
       DataList1.DataBind();
        }
  6. Create a method for the OnCancelCommand event that sets the EditItemIndex property to -1, and then rebind to the data source:
    protected void DataList1_OnCancelCommand(object Source, 
       DataListCommandEventArgs e)
    {
       DataList1.EditItemIndex = -1; // Switch out of edit mode
       DataList1.DataBind();
    }

See Also

Introduction to the DataList Web Control | Allowing Users to Delete Items | Allowing Users to Select Items | Responding to Button Events in DataList Items