다음을 통해 공유


방법: GridView 컨트롤의 단추 이벤트에 응답

업데이트: 2007년 11월

GridView 컨트롤에서 단추를 클릭하면 RowCommand 이벤트가 발생합니다. GridView 컨트롤에서는 편집, 삭제, 페이징 등의 작업에 사용할 수 있는 기능을 기본적으로 제공합니다. 또한 단추를 추가하고 RowCommand 이벤트를 사용하여 사용자 지정 기능을 컨트롤에 추가할 수 있습니다.

다음과 같은 방식으로 사용자 지정 기능을 GridView 컨트롤에 추가할 수 있습니다.

이벤트 처리기 메서드에서 이벤트 인수의 CommandName 속성을 사용하여 단추의 기능을 식별할 수 있습니다. ButtonField 또는 TemplateField 개체로 작업하는 경우에는 CommandArgument 속성을 사용하여 현재 행을 식별할 수도 있습니다. ButtonField 개체를 사용하는 경우에는 CommandArgument 속성이 행 인덱스로 자동 설정됩니다. TemplateField 개체를 사용하는 경우에는 CommandArgument 속성이 컨트롤에 의해 자동으로 설정되지 않습니다. 이때 이벤트 처리기의 행 인덱스를 확인해야 하는 경우 데이터 바인딩 식을 사용하여 단추의 CommandArgument 속성을 행 인덱스로 설정할 수 있습니다.

GridView 컨트롤의 단추 이벤트에 응답하려면

  1. 단추의 CommandName 속성을 "인쇄"나 "복사"와 같이 기능을 나타내는 문자열로 설정합니다.

  2. TemplateField 개체를 사용하고 이벤트 처리기 메서드의 행 인덱스에 액세스해야 하는 경우 단추의 CommandArgument 속성을 현재 행을 식별하는 식으로 설정합니다.

    다음 예제에서는 TemplateField 열에 있는 단추의 CommandArgument 속성을 현재 행 인덱스로 설정하는 방법을 보여 줍니다. 이 예제에서 열에는 장바구니를 나타내는 Button 컨트롤이 포함되어 있습니다.

    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton"  
          CommandName="AddToCart" 
          CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
    
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton"  
          CommandName="AddToCart" 
          CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
    
  3. GridView 컨트롤의 RowCommand 이벤트에 대한 메서드를 만듭니다. 메서드에서 다음과 같은 작업을 수행합니다.

    1. 이벤트 인수 개체의 CommandName 속성을 조사하여 전달된 명령 문자열을 확인합니다.

    2. 필요한 경우 CommandArgument 속성을 사용하여 단추가 포함된 행의 인덱스를 검색합니다.

    3. 사용자가 클릭한 단추에 대한 적절한 논리를 수행합니다.

    다음 예제에서는 GridView 컨트롤에서 단추를 클릭했을 때 응답하는 방법을 보여 줍니다. 이 예제에서는 TemplateField 열의 단추에서 "AddToCart" 명령을 보냅니다. RowCommand 이벤트 처리기는 클릭된 단추를 확인합니다. 단추가 장바구니 단추인 경우 코드에서는 적절한 논리를 수행합니다.

    Protected Sub GridView1_RowCommand(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
      If (e.CommandName = "AddToCart") Then
        ' Retrieve the row index stored in the CommandArgument property.
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
        ' Retrieve the row that contains the button 
        ' from the Rows collection.
        Dim row As GridViewRow = GridView1.Rows(index)
    
        ' Add code here to add the item to the shopping cart.
    
      End If
    End Sub
    
    protected void GridView1_RowCommand(object sender, 
      GridViewCommandEventArgs e)
    {
      if (e.CommandName == "AddToCart")
      {
        // Retrieve the row index stored in the 
        // CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);
    
        // Retrieve the row that contains the button 
        // from the Rows collection.
        GridViewRow row = GridView1.Rows[index];
    
        // Add code here to add the item to the shopping cart.
      }
    
      }
    

    ButtonField 클래스를 사용하는 예제를 보려면 GridView.RowCommand 이벤트 설명서를 참조하십시오.

참고 항목

작업

방법: DataList 또는 Repeater 항목에서 단추 이벤트에 대한 응답

참조

GridView 웹 서버 컨트롤 개요