2011-12-29 11 views
0

私はこのGridViewの持っている:「ダウンロード」ボタンが押されたときにあなたが見ることができるようにGridViewはクリックされた行の値を識別しますか?

<div class="content"> 
    <asp:GridView ID="DocumentGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="DocumentGrid_RowCommand" > 
     <Columns> 
      <asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/> 
      <asp:ButtonField HeaderText="Download Link" Text="Download"/> 
     </Columns> 
    </asp:GridView> 
</div> 

は、DocumentGrid_RowCommandが呼び出され、どのように私は値がクリックされた行である何かを見つけることができますか?

+0

を。たぶん[this](http://stackoverflow.com/questions/8363202/in-c-how-can-i-reference-a-specific-product-record-based-on-a-button-thats-cl)助けて。 –

+0

またはthis http://stackoverflow.com/questions/5619630/getting-boundfield-value-in-gridview-row-command –

答えて

1

GridViewに複数のボタンフィールドがある場合は、CommandName属性を設定します。これにより、RowCommandイベントでどのボタンが押されたかを判断できます。したがって、常にcommandName属性を設定してください。 RowCommandイベントハンドラで

<Columns> 
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/> 
<asp:ButtonField HeaderText="Download Link" Text="Download" CommandName="cmd"/> 
</Columns> 

は、GridViewCommandEventArgs.CommandArgumentプロパティボタンが押下された行のインデックスを返します。

protected void DocumentGrid_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "cmd") 
     { 
      int index = int.Parse(e.CommandArgument.ToString()); 
      GridViewRow row = DocumentGrid.Rows[index]; 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       Response.Write(row.Cells[0].Text); 
      } 
     } 
    } 
1

あなたは、このようなコードビハインドあなたに

 <Columns> 
      <asp:TemplateField HeaderText="Download"> 
       <ItemTemplate> 
        <asp:Button ID="btnDownload" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' 
         runat="server" Text="Download" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 

をマークアップを設定した場合、このようなCommandArgument確認することができます:私はしばらく前にここに同様の質問をし

if (e.CommandName == "Download") 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
} 
関連する問題