2011-09-17 10 views
3

グリッドビューの列を生成しています(このページにはAutoGenerateColumns="false"が設定されています)。htmlに定義されている列はありませんマークアップ。ASP.Net GridView列をボタンフィールドにプログラムで設定する

私は、列の1つをButtonFieldにしたいので、コードでRowCommandハンドラを使用できます。プログラム上、ButtonFieldを列にする方法はありますか?

答えて

6

ButtonFieldのプログラムによる追加:

var buttonField = new ButtonField 
         { 
          ButtonType = ButtonType.Button, 
          Text = "My button", 
          CommandName = "DoSomething", 
         }; 
Grid.Columns.Add(buttonField); 

マークアップ:

<asp:GridView runat="server" ID="Grid" AutoGenerateColumns="false" OnRowCommand="RowCommandHandler"></asp:GridView> 

ハンドラ:

protected void RowCommandHandler(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "DoSomething") 
    { 
     // place code here 
    } 
} 
関連する問題