下拉列表需要显示具有不同文本颜色的元素。颜色由服务器端决定。用途<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/