Export (0) Print
Expand All

Responding to Button Events in DataList, Repeater, or DataGrid Items

If your DataList, Repeater, or DataGrid control templates include Button, LinkButton, or ImageButton Web Server controls, then these buttons can bubble their click events to the containing DataList, Repeater, or DataGrid control. This allows you to include buttons for functions not already defined for the DataList and DataGrid controls (edit, delete, update, and cancel) and to define functionality for the Repeater control.

To respond to button events in DataList, Repeater, and DataGrid controls

  1. Include a Button, LinkButton, or ImageButton in a control template. Set the button's CommandName property to a string that identifies its function, such as "sort" or "copy".
  2. Create a method for the ItemCommand event of the control. In the method, do the following:
    1. Check the CommandName property of the event-argument object's CommandSource property to see what string was passed.
    2. Perform the appropriate logic for the button that the user clicked.
      Note   If you call the DataBind method of the parent control (the DataList, Repeater, or DataGrid control), the ItemCommand event is not raised, because the contents of the parent control are reset. Therefore, you generally do not want to call the DataBind method on each round trip (that is, in the page initialization without checking for a post back). For details, see Data Binding Multi-Record Web Server Controls and Data Binding Single-Value Web Server Controls at Design Time.

      The following example shows how you can respond to a button click in a DataList control. In the example, the ItemTemplate contains an ImageButton control that displays a shopping cart. The button sends the command AddToCart. The ItemCommand event handler determines which button was clicked, and — if it was the shopping cart button — performs the appropriate logic.

      ' Visual Basic
      Private Sub DataList1_ItemCommand(ByVal source As Object, _
      ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
      Handles DataList1.ItemCommand
         If (e.CommandName = "AddToCart") Then
            ' Add code here to add the item to the shopping cart.
            ' Use the value of e.Item.ItemIndex to find the data row
            ' in the data source.
          End If
      End Sub
      
      // C#
      private void DataList1_ItemCommand(object source, 
      System.Web.UI.WebControls.DataListCommandEventArgs e)
      {
         if (e.CommandName == "AddToCart")
         {      
            // Add code here to add the item to the shopping cart.
            // Use the value of e.Item.ItemIndex to find the data row
            // in the data source.
         }
      }
      

For an example using the DataList Web server control, see Allowing Users to Select Items in a DataList Web Server Control.

See Also

Introduction to the DataList Web Server Control | Introduction to the Repeater Web Server Control | Introduction to the DataGrid Web Server Control | Allowing Users to Delete Items in a DataList Web Server Control | Allowing Users to Edit Items in a DataList Web Server Control | Allowing Users to Select Items in a DataList Web Server Control | Creating Event Handlers in Web Forms Pages

Show:
© 2015 Microsoft