各行に7つのボタンフィールドと2つのバインドフィールドで満たされたグリッドビューを使用しています。クリックするたびにボタンフィールドの背景色を変更しようとしており、spでデータベースの更新を行っています。行コマンドでそれを取得しようとしましたが、ボタン オブジェクトの null 参照があります。また、この件名への参照をいくつか見つけようとしましたが、テンプレートフィールドにボタンが含まれている要素を見つけただけで、テンプレートフィールドではなくボタンフィールドを使用したいと思います。
これが私の gridview の行サンプルです:
<asp:ButtonField DataTextField="datechg_1" HeaderText="Date chg 1" Text="Btn1" ButtonType="Button" CommandName="chgColor"></asp:ButtonField>
<asp:ButtonField DataTextField="datechg_2" HeaderText="Date chg 2" Text="Btn2" ButtonType="Button" CommandName="chgColor"></asp:ButtonField>
<asp:ButtonField DataTextField="datechg_3" HeaderText="Date chg 3" Text="Btn3" ButtonType="Button" CommandName="chgColor"></asp:ButtonField>
null参照をスローするrowcommandコードは次のとおりです。
protected void grid_date_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="chgColor")
{
Button btn = e.CommandSource as Button; << null reference here -_-
var color = btn.Style.Value;
switch (color)
{
case "background-color:red;":
btn.Style["background-color"] = "rgb(0,200,83)";
break;
case "background-color:blue;":
btn.Style["background-color"] = "rgb(242,101,34)";
break;
case "background-color:green;":
btn.Style["background-color"] = "rgb(229,57,53)";
break;
}
}
}
スイッチの色がイマイチかと思いますが、今のところ問題ありません。ボタンをクリックする方法を知りたいです。ご協力ありがとうございました :)