CommandName Property
Collapse the table of content
Expand the table of content

ButtonField.CommandName Property

Gets or sets a string that represents the action to perform when a button in a ButtonField object is clicked.

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

public virtual string CommandName { get; set; }

Property Value

Type: System.String
The name of the action to perform when a button in the ButtonField is clicked.

Use the CommandName property to associate a command name, such as "Add" or "Remove", with the buttons in the ButtonField object. You can set the CommandName property to any string that identifies the action to perform when the command button is clicked. You can then programmatically determine the command name in an event handler and perform the appropriate actions.

NoteNote

All buttons in a ButtonField object share the same command name.

Data-bound controls recognize certain command names and automatically raise and handle the appropriate events for the control. The following command names are recognized:

  • "Cancel"

  • "Delete"

  • "Edit"

  • "Insert"

  • "New"

  • "Page"

  • "Select"

  • "Sort"

  • "Update"

To invoke paging, set the CommandName to "Page" and the CommandArgument of the contained Button control to "First", "Last", "Prev", "Next", or a page number. However, since the CommandArgument for a ButtonField control is always the integer row index, a ButtonField control is not suitable for invoking paging. Similarly, while you can invoke sorting by setting the CommandName to "Sort", the CommandArgument property for a ButtonField control is always the integer row index. For this reason, a ButtonField control is not suitable for invoking sorting. For custom command names, such as "Add" and "Remove", you need to write the event code to check the command name and perform some custom action. For more information, see DataControlCommands.

NoteNote

Not all command names are recognized by every data-bound controls. For example, "New" is not recognized by the GridView control and "Select" is not recognized by the DetailsView control.

The value of this property is stored in view state.

The following code example demonstrates how to use the CommandName property to specify a command name for the buttons in a ButtonField object of a GridView control.



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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {

    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Select")
    {

      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    

      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  

      // Display the selected author.
      Message.Text = "You selected " + contact + ".";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>ButtonField Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="CustomersGridView"/>

      <!-- Populate the Columns collection declaratively. -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="CustomersGridView_RowCommand"
        runat="server">

        <columns>

          <asp:buttonfield buttontype="Button" 
            commandname="Select"
            headertext="Select Customer" 
            text="Select"/>
          <asp:boundfield datafield="CompanyName" 
            headertext="Company Name"/>
          <asp:boundfield datafield="ContactName" 
            headertext="Contact Name"/>

        </columns>

      </asp:gridview>

        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Northwind sample database.                   -->
        <asp:sqldatasource id="CustomersSqlDataSource"  
          selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
          runat="server">
        </asp:sqldatasource>

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



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2016 Microsoft