Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
How to change colour of CheckBoxList Items Text ?
Ex: chkblstRoleFunc1.Attributes.Add("style", "color:red");

But For above ex, red colour is applied to the Text of all the Items of CheckBoxList. I want it to be applicable for only one Item among all the Items i.e. Red Colour for only one Item and rest all Items in Black Colour(which is the default colour).


Please help me out for the following code regarding Above Problem:
To Change the style Of CheckBoxList, I have called javascript from Code Behind.
Problem is that,
1) below written call to Javascript is not Working.
2) Also Let me know whether this is the right way(code) of Changing the Style of Item of CheckBoxList ?

Please have a look at the below given code :

1) Code Behind :-
if (ds1.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
      chkblstRoleFunc1.Items.Insert(i, new     ListItem(ds1.Tables[0].Rows[i].ItemArray[3].ToString(), ds1.Tables[0].Rows[i].ItemArray[1].ToString()));

if (Convert.ToInt32(ds1.Tables[0].Rows[i].ItemArray[2]) == 0)
{
      chkblstRoleFunc1.Attributes.Add"onload",    javascript:setCheckboxColor('" +  ds1.Tables[0].Rows[i].ItemArray[3].ToString() + "')");
}
}
}


2)

<style type="text/css">
.selectedCheckbox
{
color: Red;
}
</style>




XML
<script type="text/javascript">
function setCheckboxColor(selected) {
var chkList = document.getElementById('<%=chkblstRoleFunc1.ClientID%>').getElementsByTagName('label');
alert('HI');
for (var c = 0; c < chkList.length; c++) {
chkList[c].className = 'selectedCheckbox';
}
}
</script>


Thanks In Advance,
Posted
Updated 23-Sep-10 21:23pm
v3

1 solution

Try:
One can access an individual item and work on it. This is what one need to do for it:
We need to define a Databound handler for the CheckListBox. During databind, the execution will go through this method then.
C#
protected void lstMultipleValues_DataBound(object sender, EventArgs e)
{
    foreach (ListItem item in lstMultipleValues.Items)
    {
        //check anything out here, select specific ones like
        if (item.Text.StartsWith("B"))
           item.Attributes.Add("style", "color:red");
    }
}
 
Share this answer
 
Comments
shwetavc30 24-Sep-10 2:57am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
shwetavc30 24-Sep-10 3:02am    
Hi Sandeep,
Thanks a lot for ur help !! :)
Sandeep Mewara 24-Sep-10 5:09am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900