Udostępnij za pośrednictwem


ButtonField.CommandName Właściwość

Definicja

Pobiera lub ustawia ciąg reprezentujący akcję do wykonania po kliknięciu ButtonField przycisku w obiekcie.

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

Wartość właściwości

Nazwa akcji, która ma być wykonywana po kliknięciu przycisku w obiekcie ButtonField .

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą CommandName właściwości określić nazwę polecenia dla przycisków w ButtonField obiekcie kontrolki GridView .


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

Uwagi

CommandName Użyj właściwości , aby skojarzyć nazwę polecenia, taką jak "Add" lub "Remove", z przyciskami w ButtonField obiekcie . Właściwość można ustawić CommandName na dowolny ciąg, który identyfikuje akcję do wykonania po kliknięciu przycisku polecenia. Następnie można programowo określić nazwę polecenia w procedurze obsługi zdarzeń i wykonać odpowiednie akcje.

Uwaga

Wszystkie przyciski w ButtonField obiekcie mają taką samą nazwę polecenia.

Kontrolki powiązane z danymi rozpoznają określone nazwy poleceń i automatycznie zgłaszają i obsługują odpowiednie zdarzenia dla kontrolki. Rozpoznawane są następujące nazwy poleceń:

  • "Cancel"

  • "Delete"

  • "Edit"

  • "Insert"

  • "New"

  • "Page"

  • "Select"

  • "Sort"

  • "Update"

Aby wywołać stronicowanie, ustaw parametr CommandName na "Page" i CommandArgument zawartą Button kontrolkę na "First", "Prev""Last", , "Next"lub numer strony. Jednak ponieważ kontrolka CommandArgument dla kontrolki jest zawsze indeksem ButtonField wierszy całkowitych, kontrolka ButtonField nie jest odpowiednia do wywoływania stronicowania. Podobnie, podczas gdy można wywołać sortowanie, ustawiając CommandName"Sort"na wartość , CommandArgument właściwość kontrolki jest zawsze indeksem ButtonField wiersza liczby całkowitej. Z tego powodu kontrolka ButtonField nie jest odpowiednia do wywoływania sortowania. W przypadku niestandardowych nazw poleceń, takich jak "Add" i "Remove", należy napisać kod zdarzenia, aby sprawdzić nazwę polecenia i wykonać akcję niestandardową. Aby uzyskać więcej informacji, zobacz DataControlCommands.

Uwaga

Nie wszystkie nazwy poleceń są rozpoznawane przez wszystkie kontrolki powiązane z danymi. Na przykład "New" nie jest rozpoznawany przez kontrolkę GridView i "Select" nie jest rozpoznawany przez kontrolkę DetailsView .

Wartość tej właściwości jest przechowywana w stanie widoku.

Dotyczy