Export (0) Print
Expand All
Expand Minimize

GridViewCommandEventHandler Delegate

Note: This delegate is new in the .NET Framework version 2.0.

Represents the method that handles the RowCommand event of a GridView control.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public delegate void GridViewCommandEventHandler (
	Object sender,
	GridViewCommandEventArgs e
)
/** @delegate */
public delegate void GridViewCommandEventHandler (
	Object sender, 
	GridViewCommandEventArgs e
)
JScript supports the use of delegates, but not the declaration of new ones.

Parameters

sender

The source of the event.

e

A GridViewCommandEventArgs object that contains the event data.

The RowCommand event is raised when a button within the GridView control is clicked. This allows you to provide an event-handling method that performs a custom routine whenever this event occurs.

NoteNote

The GridView control also raises other specialized events when certain buttons are clicked (buttons with the CommandName property set to "Delete", "Update", and "Page" for example). When using one of these buttons, you should consider handling one of the specialized events provided by the control (such as RowDeleted or RowDeleting).

When you create a GridViewCommandEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following example demonstrates how to programmatically add a GridViewCommandEventHandler delegate to the RowCommand event of a GridView control.


<%@ Page language="C#" %>

<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new GridView object.
    GridView customersGridView = new GridView();

    // Set the GridView object's properties.
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSource";
    customersGridView.AutoGenerateColumns = false;

    // Dynamically create the columns for the GridView control.
    ButtonField addColumn = new ButtonField();
    addColumn.CommandName = "Add";
    addColumn.Text = "Add";
    addColumn.ButtonType = ButtonType.Link;

    BoundField companyNameColumn = new BoundField();
    companyNameColumn.DataField = "CompanyName";
    companyNameColumn.HeaderText = "Company Name";

    BoundField cityColumn = new BoundField();
    cityColumn.DataField = "City";
    cityColumn.HeaderText = "City";

    // Add the columns to the Columns collection
    // of the GridView control.
    customersGridView.Columns.Add(addColumn);
    customersGridView.Columns.Add(companyNameColumn);
    customersGridView.Columns.Add(cityColumn);

    // Programmatically register the event handling methods.
    customersGridView.RowCommand += new GridViewCommandEventHandler(this.CustomersGridView_RowCommand);
    customersGridView.RowCreated += new GridViewRowEventHandler(this.CustomersGridView_RowCreated);

    // Add the GridView object to the Controls collection
    // of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView);

  }

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple ButtonField columns are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked
      // by the user from the Rows collection. Use the
      // CommandSource property to access the GridView control.
      GridView customersGridView = (GridView)e.CommandSource;
      GridViewRow row = customersGridView.Rows[index];

      // Create a new ListItem object for the customer in the row.
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text);

      // If the author is not already in the ListBox, add the ListItem
      // object to the Items collection of a ListBox control.
      if(!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    // The GridViewCommandEventArgs class does not contain a
    // property that indicates which row's command button was
    // clicked. To identify which row was clicked, use the button's
    // CommandArgument property by setting it to the row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }

</script>

<html>
  <body>
    <form runat="server">

      <h3>GridViewCommandEventHandler Example</h3>

      <table width="100%">
        <tr>
          <td width="50%">
            <asp:placeholder id="GridViewPlaceHolder"
              runat="Server"/>
          </td>

          <td valign="top" width="50%">
             Customers: <br/>
             <asp:listbox id="CustomersListBox"
               runat="server"/>
          </td>
        </tr>
      </table>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>


The following example demonstrates how to declaratively add a GridViewCommandEventHandler delegate to the RowCommand event of a GridView control.


<%@ Page language="C#" %>

<script runat="server">

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
            
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];
            
      // Create a new ListItem object for the customer in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text);
            
      // If the customer is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }           
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    
    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use 
    // the button's CommandArgument property by setting it to the 
    // row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
          
      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }
    
</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridView RowCommand Example</h3>
            
      <table width="100%">         
        <tr>                
          <td width="50%">
                    
            <asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">
                
              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
                  headertext="City"/>         
              </columns>
                
            </asp:gridview>
                    
          </td>
                    
          <td valign="top" width="50%">
                    
            Customers: <br/>
            <asp:listbox id="CustomersListBox"
              runat="server"/> 
                    
          </td>  
        </tr>      
      </table>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft