Click here to Skip to main content
12,998,892 members (53,215 online)
Rate this:
Please Sign up or sign in to vote.
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() + "')");


<style type="text/css">
color: Red;

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

Thanks In Advance,
Posted 23-Sep-10 1:26am
Updated 23-Sep-10 21:23pm

1 solution

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

Solution 1

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");
shweta.chikhalikar 24-Sep-10 2:57am
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
shweta.chikhalikar 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 24 Sep 2010
Copyright © CodeProject, 1999-2017
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