Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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>
 

 
<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 23-Sep-10 1:26am
Edited 23-Sep-10 21:23pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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");
    }
}
  Permalink  
Comments
shweta.chikhalikar at 24-Sep-10 2:57am
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
shweta.chikhalikar at 24-Sep-10 3:02am
   
Hi Sandeep,
Thanks a lot for ur help !! :)
Sandeep Mewara at 24-Sep-10 5:09am
   
:)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 24 Sep 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100