Reputation: 49
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 ??
}
}
Upvotes: 0
Views: 246
Reputation: 7592
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");
Upvotes: 1
Reputation: 709
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
}
});
Upvotes: 0