更改列表属性(背景色,底色,文本大小等)

本文关键字:底色 文本 小等 背景色 列表 属性 | 更新日期: 2023-09-27 17:55:01

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

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
    }
});