Navigation

Search

Categories

On this page

The CommandName and CommandArgument Properties

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 0
This Week: 0
Comments: 0

Sign In

 Tuesday, December 11, 2007
Tuesday, December 11, 2007 3:02:31 PM (Eastern Standard Time, UTC-05:00) ( )


This example shows a GridView control that displays some rows from a database table. The <Columns> section adds two ButtonField columns and a TemplateField column. The first ButtonField column generates a normal Button control every each row, because it has the ButtonType="Button" attribute. The second ButtonField column, which does not contain this attribute, generates the default of a LinkButton in every row.

The ButtonField column exposes a CommandName property, declared as "SendButtonField" for the first column and "CopyButtonField" for the second column. The TemplateField column contains both a Button and a LinkButton control, with the CommandName properties set to DetailsButton and DetailsLinkButton respectively.

When declaring individual button-type controls, you can also set the CommandArgument property. In this example, the CommandArgument for both buttons is the value of the CategoryName and CategoryID columns of the current row generated with Eval data-binding statements.

ASP.NET automatically wires up the CommandArgument of the buttons in the ButtonField columns to the RowIndex of each row in the GridView control. This means that you can extract the row index, and use it to access the rows or the other properties of the GridView. This example sets the DataKeyNames property of the GridView to the CategoryName column through the attribute DataKeyNames="CategoryName". This means that you can access the value of the DataKey for the current row using the index returned.

<script runat="server">

  protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    Label1.Text = "CommandName '<b>" + e.CommandName + "</b>' detected in Row_Command event.<br />";
    Label1.Text += "CommandArgument = '<b>" + e.CommandArgument + "</b>'.<br />";
    Label1.Text += "CommandSource = '<b>" + e.CommandSource.ToString() + "</b>'.<br />";
    if (e.CommandName.IndexOf("ButtonField") > 0)
    {
      Int32 rowIndex = Int32.Parse(e.CommandArgument.ToString());
      Label1.Text += "DataKeys[0] = '<b>" + GridView1.DataKeys[rowIndex].Value + "</b>'.";
    }
  }
</script>

  <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CategoryName"
    DataSourceID="SqlDataSource1" GridLines="Vertical" OnRowCommand="GridView1_RowCommand">
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <Columns>
      <asp:ButtonField HeaderText="ButtonField" Text="Send"
            CommandName="SendButtonField" ButtonType="Button" />
      <asp:ButtonField HeaderText="ButtonField" Text="Copy"
            CommandName="CopyButtonField" />
      <asp:TemplateField HeaderText="TemplateField">
        <ItemTemplate>
          <asp:Button runat="server" ID="Button1"
                Text="Details" CommandName="DetailsButton"
                CommandArgument='<%# Eval("CategoryName")
                      + " [" + Eval("CategoryID") + "]" %>' />
          <asp:LinkButton runat="server" ID="Button2"
                Text="Details" CommandName="DetailsLinkButton"
                CommandArgument='<%# Eval("CategoryName")
                      + " [" + Eval("CategoryID") + "]" %>' />
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#DCDCDC" />
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectString %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT CategoryID, CategoryName FROM Categories">
  </asp:SqlDataSource>
  <p />
  <asp:Label ID="Label1" runat="server" EnableViewState="false" />