changing list properties (back color, underling, text size etc)

233 views Asked by At

I'm trying to change the text properties within a list box when the list item is checked. Currently if i check an item it will print a message in the label so at least its registering a selection. Any ideas on how i can change the text properties i.e. if an item is selected then change the color of the text on the selected item from default black to red?

for (int i = 0; i < checklist.Items.Count; i++)
    {
        if (checkist.Items[i].Selected)
        {
            lbltest.Text = "yayee";
            //checklist.Items[i].Attributes.CssStyle(); maybee ??
        }
    }
2

There are 2 answers

6
mclark1129 On BEST ANSWER

Because CheckboxList.Items is a collection of ListItems I think your best bet is to use CssStyle to add individual style attributes.

for (int i = 0; i < checklist.Items.Count; i++)
{
    if (checkist.Items[i].Selected)
    {
        checklist.Items[i].Attributes.CssStyle.Add("color", "red");         
    }
}

Any other modifications, such as underline or bold could be made in a similar fashion. You just have to use standard CSS:

// Bold
checklist.Items[i].Attributes.CssStyle.Add("font-weight", "bold");  

// Underline
checklist.Items[i].Attributes.CssStyle.Add("text-decoration", "underline");  
0
bateloche On

The easier and faster way to do that is on client side with java script, you can use a simple jQuery function for that.

$("#checklistId > checkbox").change(function(){
    if($(this).is(":checked")) {
         //Add the style
    }
    else {
        //Add the unchecked style
    }
});