Freigeben über


ButtonField.CommandName Eigenschaft

Definition

Dient zum Abrufen oder Festlegen einer Zeichenfolge, die die auszuführende Aktion darstellt, wenn auf eine Schaltfläche in einem ButtonField Objekt geklickt wird.

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

Eigenschaftswert

Der Name der Aktion, die ausgeführt werden soll, wenn auf eine Schaltfläche in der ButtonField Schaltfläche geklickt wird.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die CommandName Eigenschaft verwendet wird, um einen Befehlsnamen für die Schaltflächen in einem ButtonField Objekt eines GridView Steuerelements anzugeben.


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

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

    Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
  
        ' If multiple ButtonField column fields are used, use the
        ' CommandName property to determine which button was clicked.
        If e.CommandName = "Select" Then
    
            ' Convert the row index stored in the CommandArgument
            ' property to an Integer.
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
            ' Get the last name of the selected author from the appropriate
            ' cell in the GridView control.
            Dim selectedRow As GridViewRow = CustomersGridView.Rows(index)
            Dim contactCell As TableCell = selectedRow.Cells(1)
            Dim contact As String = contactCell.Text
    
            ' Display the selected author.
            Message.Text = "You selected " & contact & "."
      
        End If
    
    End Sub
    
</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>

Hinweise

Verwenden Sie die CommandName Eigenschaft, um einen Befehlsnamen, z "Add" . B. oder "Remove", den Schaltflächen im ButtonField Objekt zuzuordnen. Sie können die CommandName Eigenschaft auf eine beliebige Zeichenfolge festlegen, die die auszuführende Aktion identifiziert, wenn auf die Befehlsschaltfläche geklickt wird. Anschließend können Sie den Befehlsnamen in einem Ereignishandler programmgesteuert ermitteln und die entsprechenden Aktionen ausführen.

Hinweis

Alle Schaltflächen in einem ButtonField Objekt verwenden denselben Befehlsnamen.

Datengebundene Steuerelemente erkennen bestimmte Befehlsnamen und lösen automatisch die entsprechenden Ereignisse für das Steuerelement aus. Die folgenden Befehlsnamen werden erkannt:

  • "Cancel"

  • "Delete"

  • "Edit"

  • "Insert"

  • "New"

  • "Page"

  • "Select"

  • "Sort"

  • "Update"

Zum Aufrufen der Seitenverwaltung legen Sie den Wert "Page" für CommandArgument das CommandName enthaltene Button Steuerelement auf "First", "Last", , "Prev", oder "Next"eine Seitenzahl fest. Da das CommandArgument Steuerelement jedoch ButtonField immer der Ganzzahlzeilenindex ist, eignet sich ein ButtonField Steuerelement nicht für das Aufrufen von Paging. Ebenso können Sie die Sortierung aufrufen, indem Sie den CommandName Wert auf "Sort""" festlegen, ist die CommandArgument Eigenschaft für ein ButtonField Steuerelement immer der Ganzzahlzeilenindex. Aus diesem Grund eignet sich ein ButtonField Steuerelement nicht zum Aufrufen der Sortierung. Für benutzerdefinierte Befehlsnamen, z "Add" . B. und "Remove", müssen Sie den Ereigniscode schreiben, um den Befehlsnamen zu überprüfen und eine benutzerdefinierte Aktion auszuführen. Weitere Informationen finden Sie unter DataControlCommands.

Hinweis

Nicht alle Befehlsnamen werden von allen datengebundenen Steuerelementen erkannt. Beispielsweise "New" wird das GridView Steuerelement nicht erkannt und "Select" vom DetailsView Steuerelement nicht erkannt.

Der Wert dieser Eigenschaft wird im Ansichtszustand gespeichert.

Gilt für: