다음을 통해 공유


ButtonField.CommandName 속성

정의

ButtonField 개체의 단추를 클릭할 때 수행할 작업을 나타내는 문자열을 가져오거나 설정합니다.

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

ButtonField의 단추를 클릭할 때 수행할 작업의 이름입니다.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다.는 CommandName 에 있는 단추의 명령 이름을 지정 하는 속성을 ButtonField 의 개체를 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>

설명

사용 하 여는 CommandName 와 같은 명령 이름을 연결할 속성을 "Add" 또는 "Remove"에서 단추를 사용 하 여는 ButtonField 개체입니다. 설정할 수 있습니다는 CommandName 명령 단추를 클릭할 때 수행할 동작을 식별 하는 모든 문자열 속성입니다. 그런 다음 프로그래밍 방식으로 이벤트 처리기에서 명령 이름을 확인 하 수 적절 한 작업을 수행 합니다.

참고

모든 단추에는 ButtonField 개체 같은 명령 이름을 공유 합니다.

데이터 바인딩된 컨트롤 특정 명령 이름을 인식 하 고 자동으로 발생 하 고이 컨트롤에 대 한 적절 한 이벤트를 처리할 만듭니다. 다음 명령은 이름이 인식 됩니다.

  • "Cancel"

  • "Delete"

  • "Edit"

  • "Insert"

  • "New"

  • "Page"

  • "Select"

  • "Sort"

  • "Update"

페이징을 호출 하기 위해 설정 합니다 CommandName"Page"CommandArgument 의 포함 된 Button 컨트롤을 "First", "Last", "Prev", "Next", 또는 페이지 번호를 합니다. 그러나를 CommandArgument 에 대 한는 ButtonField 컨트롤은 항상 정수 행 인덱스를 ButtonField 컨트롤 페이징 호출에 적합 하지 않습니다. 마찬가지로, 정렬을 설정 하 여 호출할 수는 CommandName"Sort", CommandArgument 속성에 대 한를 ButtonField 컨트롤은 항상 정수 행 인덱스입니다. 이러한 이유로 ButtonField 컨트롤 정렬 호출에 적합 하지 않습니다. 사용자 지정에 대 한 명령 이름와 같은 "Add""Remove", 명령 이름을 확인 하 고 일부 사용자 지정 작업을 수행 하는 이벤트 코드를 작성 해야 합니다. 자세한 내용은 DataControlCommands를 참조하세요.

참고

일부 명령 이름 마다 데이터 바인딩된 컨트롤에서 인식 됩니다. 예를 들어 "New" 에서 인식 되지 않습니다 합니다 GridView 컨트롤 및 "Select" 에서 인식 되지 않습니다는 DetailsView 제어 합니다.

이 속성의 값은 뷰 상태에 저장 됩니다.

적용 대상