次の方法で共有


方法 : GridView コントロールのボタン イベントに応答する

更新 : 2007 年 11 月

GridView コントロール内のボタンがクリックされると、RowCommand イベントが発生します。GridView コントロールには、編集、削除、ページング機能などの操作をサポートする機能が組み込まれています。また、ボタンを追加して RowCommand イベントを使用することにより、コントロールにカスタム機能を追加することもできます。

カスタム機能を GridView コントロールに追加するには、次の方法で行います。

イベント引数の CommandName プロパティを使用して、イベント ハンドラ メソッドでボタンの機能を識別できます。ButtonField オブジェクトまたは TemplateField オブジェクトを使用する場合には、CommandArgument プロパティを使用して、現在の行を識別できます。ButtonField オブジェクトを使用する場合には、CommandArgument プロパティは自動的に行インデックスに設定されます。TemplateField オブジェクトを使用する場合には、CommandArgument プロパティはコントロールによって自動的には設定されません。ここで、イベント ハンドラ内で行インデックスを決定する必要がある場合には、データ バインド式を使用して、ボタンの CommandArgument プロパティを行インデックスに設定できます。

GridView コントロールのボタン イベントに応答するには

  1. ボタンの CommandName プロパティに、"Print" や "Copy" など、機能を識別する文字列を設定します。

  2. TemplateField オブジェクトを使用しており、イベント ハンドラ メソッド内で行インデックスにアクセスする必要がある場合は、ボタンの CommandArgument プロパティを、現在の行を識別する式に設定します。

    TemplateField 列のボタンの CommandArgument プロパティを現在の行インデックスに設定する方法を次の例に示します。例では、この列に、買い物カゴを表示する Button コントロールが含まれています。

    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton" runat="server" 
          CommandName="AddToCart" 
          CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
    
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton" runat="server" 
          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 Web サーバー コントロールの概要