共用方式為


How to: Respond to Button Events in a GridView Control

When a button is clicked in a GridView control, the RowCommand event is raised. The GridView control has built-in functionality for operations such as edit, delete, and paging. You can also add buttons and use the RowCommand event to add custom functionality to the control.

You can add custom functionality to a GridView control in the following ways:

You can use the CommandName property of the event argument to identify the button's function in the event handler method. If you are working with ButtonField or TemplateField objects, you can also use the CommandArgument property to identify the current row. When you are using a ButtonField object, the CommandArgument property is set automatically to the row index. When you are using a TemplateField object, the CommandArgument property is not automatically set by the control. In that case, if you have to determine the row index in the event handler, you can set the CommandArgument property of the button to the row index by using a data-binding expression.

To respond to button events in the GridView control

  1. Set the button's CommandName property to a string that identifies its function, such as "Print" or "Copy".

  2. If you are using the TemplateField object and have to access the row index in the event handler method, set the button's CommandArgument property to an expression that identifies the current row.

    The following example shows how you can set the CommandArgument property of a button in a TemplateField column to the current row index. In the example, the column contains a Button control that displays a shopping cart.

    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton" runat="server" 
          CommandName="AddToCart" 
          CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
    
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton" runat="server" 
          CommandName="AddToCart" 
          CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
    
  3. Create a method for the RowCommand event of the GridView control. In the method, do the following:

    1. Check the CommandName property of the event-argument object to see what string was passed.

    2. Retrieve the index of the row that contains the button by using the CommandArgument property, if required.

    3. Perform the appropriate logic for the button that the user clicked.

    The following example shows how you can respond to a button click in a GridView control. In the example, a button in a TemplateField column sends the command "AddToCart". The RowCommand event handler determines which button was clicked. If it was the shopping cart button, the code performs the appropriate logic.

    Protected Sub GridView1_RowCommand(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
      If (e.CommandName = "AddToCart") Then
        ' Retrieve the row index stored in the CommandArgument property.
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
        ' Retrieve the row that contains the button 
        ' from the Rows collection.
        Dim row As GridViewRow = GridView1.Rows(index)
    
        ' Add code here to add the item to the shopping cart.
    
      End If
    End Sub
    
    protected void GridView1_RowCommand(object sender, 
      GridViewCommandEventArgs e)
    {
      if (e.CommandName == "AddToCart")
      {
        // Retrieve the row index stored in the 
        // CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);
    
        // Retrieve the row that contains the button 
        // from the Rows collection.
        GridViewRow row = GridView1.Rows[index];
    
        // Add code here to add the item to the shopping cart.
      }
    
      }
    

    For an example that uses the ButtonField class, see the GridView.RowCommand event documentation.

See Also

Tasks

How to: Respond to Button Events in DataList or Repeater Items

Reference

GridView Web Server Control Overview