c# - ASP :DropDownList Codebehind Add Color

标签 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>

最佳答案

不要添加 style,而是添加 css class 并在 css 文件 中使用此 class 来处理颜色

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

在你的CSS中:

.blue {
    color: blue;
}

更新

因此,您需要使用 : 而不是 =

    li.Attributes.Add("style", "color:" + color);

关于c# - ASP :DropDownList Codebehind Add Color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38791489/

相关文章:

javascript - 防止输入文本框中的退格

javascript - 如何检查javascript中的div值

PHP - 检查页面是否在移动或桌面浏览器上运行

c# - 为什么Java和C#要给每个对象都加上内在锁?

c# - 处理未知数量的捕获组时仅替换一组

C# 从集合类访问对象的属性

c# - 具有 WS-Security 的 WCF 客户端

html - 在CSS中将不透明度值设置为多模态

html - 如何在 div 和 body 底部之间留一些空间?

javascript - 单击关闭选项卡