ListView.ItemCommand 이벤트

정의

ListView 컨트롤의 단추를 클릭할 때 발생합니다.

public:
 event EventHandler<System::Web::UI::WebControls::ListViewCommandEventArgs ^> ^ ItemCommand;
public event EventHandler<System.Web.UI.WebControls.ListViewCommandEventArgs> ItemCommand;
member this.ItemCommand : EventHandler<System.Web.UI.WebControls.ListViewCommandEventArgs> 
Public Custom Event ItemCommand As EventHandler(Of ListViewCommandEventArgs) 

이벤트 유형

예제

다음 예제에서는 이벤트 처리기를 만드는 ItemCommand 방법을 보여줍니다.

<%@ 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 EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "AddToList"))
    {
      // Verify that the employee ID is not already in the list. If not, add the
      // employee to the list.
      ListViewDataItem dataItem = (ListViewDataItem)e.Item;
      string employeeID = 
        EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();
      
      if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
      {
        ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
        SelectedEmployeesListBox.Items.Add(item);
      }
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Employee List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:ListView runat="server" 
        ID="EmployeesListView"
        OnItemCommand="EmployeesListView_OnItemCommand"
        DataSourceID="EmployeesDataSource" 
        DataKeyNames="EmployeeID">
        <LayoutTemplate>
          <table runat="server" id="tblEmployees" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
          <asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
            <Fields>
              <asp:NumericPagerField />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" 
                Text='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
            </td>
            <td style="width:80px">
              <asp:LinkButton runat="server" 
                ID="SelectEmployeeButton" 
                Text="Add To List" 
                CommandName="AddToList" 
                CommandArgument='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      
      <br /><br />
      <b>Selected Employees:</b><br />
      <asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
       
      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->       
      <asp:SqlDataSource ID="EmployeesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
                       FROM HumanResources.vEmployee
                       ORDER BY [LastName], [FirstName], [EmployeeID]">
      </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">
  Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, _
  ByVal e As ListViewCommandEventArgs)
    
    If String.Equals(e.CommandName, "AddToList") Then
      
      ' Verify that the employee ID is not already in the list. If not, add the
      ' employee to the list.
      Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
      Dim employeeID As String = _
        EmployeesListView.DataKeys(dataItem.DisplayIndex).Value.ToString()
      
      If SelectedEmployeesListBox.Items.FindByValue(employeeID) Is Nothing Then
        Dim item As ListItem = _
          New ListItem(e.CommandArgument.ToString(), employeeID)
        SelectedEmployeesListBox.Items.Add(item)
      End If
      
    End If
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Employee List</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:ListView runat="server" 
        ID="EmployeesListView"
        OnItemCommand="EmployeesListView_OnItemCommand"
        DataSourceID="EmployeesDataSource" 
        DataKeyNames="EmployeeID">
        <LayoutTemplate>
          <table runat="server" id="tblEmployees" 
                 cellspacing="0" cellpadding="1" width="440px" border="1">
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
          <asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
            <Fields>
              <asp:NumericPagerField />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" ID="NameLabel" 
                Text='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
            </td>
            <td style="width:80px">
              <asp:LinkButton runat="server" 
                ID="SelectEmployeeButton" 
                Text="Add To List" 
                CommandName="AddToList" 
                CommandArgument='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      
      <br /><br />
      <b>Selected Employees:</b><br />
      <asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
       
      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->       
      <asp:SqlDataSource ID="EmployeesDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
                       FROM HumanResources.vEmployee
                       ORDER BY [LastName], [FirstName], [EmployeeID]">
      </asp:SqlDataSource>
    </form>
  </body>
</html>

설명

ItemCommand 이벤트는 단추는 ListView 컨트롤을 클릭 합니다. 이렇게 하면이 이벤트가 발생할 때마다 사용자 지정 루틴을 수행할 수 있습니다.

컨트롤의 단추는 ListView 컨트롤의 기본 제공 기능을 호출할 수도 있습니다. 다음 표에서는 기본 제공 기능을 호출하기 위해 단추의 속성과 함께 CommandName 사용할 수 있는 값을 나열합니다.

CommandName 값 Description
"취소" 편집 또는 삽입 작업을 취소합니다. ItemCanceling 이벤트를 발생시킵니다.
"Delete" 데이터 소스에서 현재 레코드를 삭제합니다. 발생 합니다 ItemDeletedItemDeleting 이벤트입니다.
"Select" SelectedIndex 속성을 항목의 DisplayIndex 속성 값으로 설정합니다. 항목에 SelectedItemTemplate 대한 템플릿을 렌더링합니다. 발생 합니다 SelectedIndexChangingSelectedIndexChanged 이벤트입니다.
"Edit" 항목을 편집 모드로 전환합니다. 항목에 EditItemTemplate 대한 템플릿을 렌더링합니다. ItemEditing 이벤트를 발생시킵니다.
"Insert" 템플릿의 바인딩된 값을 InsertItemTemplate 데이터 원본에 삽입합니다. 발생 합니다 ItemInsertingItemInserted 이벤트입니다.
"업데이트" 데이터 원본의 현재 레코드를 템플릿의 바인딩된 값 EditItemTemplate 으로 업데이트. 발생 합니다 ItemUpdatingItemUpdated 이벤트입니다.
"Sort" 단추의 속성에 CommandArgument 나열된 열을 정렬합니다. 발생 합니다 SortingSorted 이벤트입니다.

ItemCommand 이전 표에 나열된 단추를 클릭할 때도 이벤트가 발생합니다. 그러나 테이블에 나열된 이벤트를 사용하는 것이 좋습니다. 이벤트는 ItemCommand 일반적으로 사용자 지정 작업을 처리하는 데 사용됩니다.

ListViewCommandEventArgs 개체가 이벤트 처리기에 전달되므로 클릭한 단추의 명령 이름과 명령 인수를 확인할 수 있습니다. 명령 이름을 확인하려면 속성을 사용합니다 CommandEventArgs.CommandName . 명령 인수를 확인하려면 속성을 사용합니다 CommandEventArgs.CommandArgument . 이벤트를 발생시킨 컨트롤에 액세스하려면 속성을 사용합니다 ListViewCommandEventArgs.CommandSource .

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보