i'm using a gridview filled with 7 buttonfields and 2 boundfields on each row. I'm trying to change the backcolor of my buttonfield on each click and doing some db update with a sp. I have tried to get that on rowcommand but having a null reference for the button object. I've also tried to find some references to this subjetc but just found elements on templatefields with buttons inside and i'd like to use a buttonfield not a template one.
Here's a row sample of my 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>
and here's the rowcommand code throwing null reference :
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;
}
}
}
The switch color isn't good at all i guess, but it's not a problem at the moment. I would like to know how to get the button clicked. Thank you for your help :)