ListViewCommandEventArgs クラス

定義

ItemCommand イベントのデータを提供します。

public ref class ListViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class ListViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type ListViewCommandEventArgs = class
    inherit CommandEventArgs
Public Class ListViewCommandEventArgs
Inherits CommandEventArgs
継承
ListViewCommandEventArgs

次の例は、イベント ハンドラーに渡される オブジェクトを ListViewCommandEventArgs 使用する方法を示しています。

<%@ 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 クリックされたときに発生します。 これにより、このイベントが発生するたびにカスタム ルーチンを実行するイベント処理メソッドを提供できます。

Note

コントロールは ListView 、特定のボタンがクリックされたときにも他のイベントを発生させます (たとえば、プロパティが CommandName "Delete"、"Update"、または "Page" に設定されているボタン)。 これらのボタンのいずれかを使用すると、 イベントや ItemDeleting イベントなどItemDeleted、コントロールによって提供される特殊なイベントのいずれかを処理できます。

ListViewCommandEventArgsオブジェクトがイベント処理メソッドに渡されます。これにより、クリックされたボタンのコマンド名とコマンド引数を決定できます。 コマンド名を確認するには、 プロパティを CommandName 使用し、コマンド引数を決定するには、 プロパティを CommandArgument 使用します。 プロパティを使用して、イベントを発生させたボタン コントロールに CommandSource アクセスすることもできます。

ListViewCommandEventArgs クラスのインスタンスの初期プロパティ値一覧については、ListViewCommandEventArgs コンストラクターに関するトピックを参照してください。

コンストラクター

ListViewCommandEventArgs(ListViewItem, Object, CommandEventArgs)

コマンドのソースとイベントの引数を指定して、ListViewCommandEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

CommandArgument

コマンドの引数を取得します。

(継承元 CommandEventArgs)
CommandName

コマンドの名前を取得します。

(継承元 CommandEventArgs)
CommandSource

コマンドのソースを取得します。

Handled

コントロールがイベントを処理したかどうかを示す値を取得または設定します。

Item

コマンドが実行された対象のデータ項目を取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください