ButtonColumn.CommandName プロパティ

定義

ButtonColumn オブジェクトのボタンがクリックされたときに実行するコマンドを表す文字列を取得または設定します。Gets or sets a string that represents the command to perform when a button in the ButtonColumn object is clicked.

public:
 virtual property System::String ^ CommandName { System::String ^ get(); void set(System::String ^ value); };
public virtual string CommandName { get; set; }
member this.CommandName : string with get, set
Public Overridable Property CommandName As String

プロパティ値

String

ButtonColumn のボタンがクリックされたときに実行するコマンドを表す文字列。A string that represents the command to perform when a button in the ButtonColumn is clicked. 既定値は、空の文字列 ("") です。The default is an empty string ("").

プロパティを使用してコマンド名をボタンに関連付ける方法を次のコード例に示し CommandName ます。The following code example demonstrates how to use the CommandName property to associate a command name with a button. 次に、クリックしたボタンのコマンド名は、イベントのイベントハンドラーでプログラムによって決定され、 ItemCommand 適切なアクションが実行されます。Then, the command name of the clicked button is determined programmatically in the event handler for the ItemCommand event and the appropriate action is performed.


<%@ 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">

    protected void Page_Init(object sender, EventArgs e)
    {
       // Create dynamic column to add to Columns collection.
       ButtonColumn AddColumn = new ButtonColumn();
       AddColumn.HeaderText="Add Item"; 
       AddColumn.Text="Add";
       AddColumn.CommandName="Add";
       AddColumn.ButtonType = ButtonColumnType.PushButton;

       // Add column to Columns collection.
       ItemsGrid.Columns.AddAt(0, AddColumn);
    }


    protected void ItemsGrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Add")
        {
            // Add logic for addition operation here.
            this.TextBox1.Text = "Added";
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Untitled Page</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" /><br />
        <asp:DataGrid 
          ID="ItemsGrid" 
          runat="server" 
          DataSourceID="CustomersSqlDataSource" 
          AutoGenerateColumns="true" 
          OnItemCommand="ItemsGrid_ItemCommand" />
        
        <!-- 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>

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

<%@ Page Language="VB" %>

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

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        ' Create dynamic column to add to Columns collection.
        Dim AddColumn As New ButtonColumn
        AddColumn.HeaderText = "Add Item"
        AddColumn.Text = "Add"
        AddColumn.CommandName = "Add"
        AddColumn.ButtonType = ButtonColumnType.PushButton

        ' Add column to Columns collection.
        ItemsGrid.Columns.AddAt(0, AddColumn)
    End Sub


    Protected Sub ItemsGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
        If e.CommandName = "Add" Then
            ' Add logic for addition operation here.
            TextBox1.Text = "Added"          
        End If
    End Sub
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="head1" runat="server">
    <title>Untitled Page</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" /><br />
        <asp:DataGrid 
          ID="ItemsGrid" 
          runat="server" 
          DataSourceID="CustomersSqlDataSource" 
          AutoGenerateColumns="true" 
          OnItemCommand="ItemsGrid_ItemCommand" />
        
        <!-- 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>

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

注釈

プロパティを使用して、やなどの CommandName コマンド名 AddRemove ボタンに関連付けます。Use the CommandName property to associate a command name, such as Add or Remove, with a button. プロパティは、 CommandName オブジェクトのボタンがクリックされたときに実行するアクションを識別する任意の文字列に設定でき System.Web.UI.WebControls.ButtonColumn ます。You can set the CommandName property to any string that identifies the action to perform when a button in the System.Web.UI.WebControls.ButtonColumn object is clicked. 次に、イベントのイベントハンドラーで、プログラムによってコマンド名を確認 ItemCommand し、適切なアクションを実行できます。Then, you can determine programmatically the command name in the event handler for the ItemCommand event and perform the appropriate actions.

このプロパティの値は、ビューステートに格納されます。The value of this property is stored in view state.

適用対象

こちらもご覧ください