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 Delete Items

You can allow users to delete items in a DataList control in a variety of ways. One way is to include a Delete button in an item and to delete that item immediately when the user clicks it.

Another way is to include a checkbox in individual items. Users can then check all the items they want and then click a separate Delete button to delete them in a batch. This is a style used in products such as Microsoft Hotmail.

To allow users to delete items individually

  1. In the ItemTemplate (and AlternatingItemTemplate, if you are using that), add a Button or LinkButton Web control and set its Text property to "Delete" or something similar. Set the Command property for this button to delete (case-sensitive).
  2. Create a method for the OnDeleteCommand event. In the method:
    • Delete the item from the data source and then rebind the DataList control. The index of the item the user clicked is available via the Item object's ItemIndex property. To get the value of individual controls, use the FindControl method of the Item event-argument object.
    • Rebind the control to its data source.

    The event-handling method might look like this:

    [C#]
    protected void DataList1_OnDeleteCommand(object Source, 
       DataListCommandEventArgs e)
    {
       string ID = ((TextBox)e.Item.FindControl("CustomerID")).Text;
    
       // Delete item from data source here using ID extracted earlier
    
       DataList1.DataBind(); // Rebind to data source
    }
    

To allow users to delete multiple items in a batch

  1. In the ItemTemplate (and AlternatingItemTemplate, if you are using that), add a CheckBox Web control and set its ID property. Make sure that the AutoPostBack property of the CheckBox control is not true.
  2. Somewhere other than in the DataList control, include a button or similar control to act as the Delete button.
  3. Create a method for the OnClick event of the Delete method. In the method:
    1. Loop through the Items collection of the DataList control, extracting each item in turn.
    2. Within the item, use the item's FindControl method to get the CheckBox control from Step 1 and test its Checked property.
    3. If the box is checked, delete the corresponding item from the data source.
    4. Rebind the DataList control to its data source.

    The following example shows how you can create an event-handling method for a Delete button that deletes items in a batch using the procedure outlined above. The example assumes that you are working with a DataList control called DataList1 and that the CheckBox control in each item is called Check1.

    protected void btnDelete_Click(object sender, System.EventArgs e)
    {
       int i;
       for(i=0; i< DataList1.Items.Count; i++){
          DataListItem NextItem = DataList1.Items[i];
          Boolean DelFlag = ((CheckBox)NextItem.FindControl("Check1")).Checked;
          if(DelFlag == true){
             
             // Delete item from data source here
             
          }
          DataList1.DataBind(); // Rebind to data source
       }
    }

See Also

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