ASP:DropDownList Codebehind添加颜色

时间:2016-08-05 14:18:29

标签: c# html css asp.net drop-down-menu

下拉列表需要显示具有不同文本颜色的项目。颜色由服务器端决定。使用<style>标签与CSS样式表。见例:

protected void Page_Load(object sender, EventArgs e)
    {
        ListItem li = CreateListItemWithColor("Hello", "myValue", "blue");
        employeeDropDownBox.Items.Add(li);

    }
public ListItem CreateListItemWithColor(string text, string value, string color)
    {
        //Create the list item based on input text/value
        ListItem li = new ListItem();
        li.Text = text;
        li.Value = value;
        li.Attributes.Add("style", "color="+color);
        return li;
    }

从我在其他SO帖子中读到的有关格式化列表项文本的内容,我的一般程序似乎很接近。但我的ListItem总是黑色的。我错过了什么?

缩写HTML:

<style>
    #employeeDropDownBox {
        height: 65px;
        width: 425px;
        font-size: 27px;
        margin-left: 5px;
    }

</style>

<div>
    <asp:DropDownList ID="employeeDropDownBox" runat="server" ></asp:DropDownList>
</div>

1 个答案:

答案 0 :(得分:1)

不添加((foo(bar(baz)))(bat)(man)) ,而是添加style&amp;在css class中使用此class来处理颜色

css file

在你的css中:

li.Attributes.Add("class", "blue"); // you can use the `color` parameter as well

<强>更新

因此,您需要使用.blue { color: blue; } 代替:

=
相关问题