更改列表属性(背景颜色、下划线、文本大小等)

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

我正在尝试在选中列表项时更改列表框中的文本属性。目前,如果我检查一个项目,它会在标签中打印一条消息,这样至少它会注册一个选择。关于如何更改文本属性的任何想法,即如果选择了一个项目,然后将所选项目上的文本颜色从默认黑色更改为红色?

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

因为 CheckboxList.ItemsListItems 的集合我认为你最好的选择是使用 CssStyle 添加单独的样式属性。

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

任何其他修改,例如下划线或粗体,都可以用类似的方式进行。你只需要使用标准 CSS:

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

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

更简单快捷的方法是在客户端使用 java 脚本,您可以为此使用一个简单的 jQuery 函数。

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