You can set the OnCheckChanged
server-side event for the check box as in markup below. Then, all you need is to write code in server-side event to change the BackColor
.
Markup
<ItemTemplate>
<asp:CheckBox ID="checkboxAttendanceStatus" BackColor="Red" runat="server"
AutoPostBack="true" OnCheckedChanged="checkboxAttendanceStatus_CheckedChanged"/>
</ItemTemplate>
C# code for server-side event
protected void checkboxAttendanceStatus_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
if (chk.Checked)
{
chk.BackColor = System.Drawing.Color.Green;
} else
{
chk.BackColor = System.Drawing.Color.Red;
}
}
Another approach using client-side code without any C#
If you wanted to implement this requirement completely on the client-side, then you could add the following JavaScript to your aspx page.
You do not need to subscribe to CheckChanged event of checkbox nor write any C# code. This approach needs only JavaScript/jQuery code.
<script type="text/javascript">
function pageLoad() {
var checkboxes = $("[id*='checkboxAttendanceStatus']");
checkboxes.each(function (i) {
if (this.checked) {
$(this).parent().css("background-color", "green");
} else {
$(this).parent().css("background-color", "red");
}
});
}
</script>