ButtonColumn.CommandName Propriété

Définition

Obtient ou définit une chaîne qui représente la commande à effectuer lorsqu'un clic a été effectué sur le bouton dans l'objet ButtonColumn.

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

Valeur de propriété

Chaîne qui représente la commande à effectuer lorsqu'un clic a été effectué sur le bouton dans ButtonColumn. La valeur par défaut est une chaîne vide ("").

Exemples

L’exemple de code suivant montre comment utiliser la CommandName propriété pour associer un nom de commande à un bouton. Ensuite, le nom de la commande du bouton cliqué est déterminé par programme dans le gestionnaire d’événements de l’événement ItemCommand et l’action appropriée est effectuée.


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

Remarques

Utilisez la CommandName propriété pour associer un nom de commande, tel que Add ou Remove, à un bouton. Vous pouvez définir la propriété sur CommandName n’importe quelle chaîne qui identifie l’action à effectuer lorsqu’un bouton de l’objet System.Web.UI.WebControls.ButtonColumn est cliqué. Ensuite, vous pouvez déterminer par programmation le nom de la commande dans le gestionnaire d’événements pour l’événement ItemCommand et effectuer les actions appropriées.

La valeur de cette propriété est stockée dans l’état d’affichage.

S’applique à

Voir aussi