[英]Styling drop down list selected item
I checked for this before posting here and didn't find anything for my problem. 我在此发布之前检查过此内容,但没有发现任何问题。
I have a drop down list to which I manually append an item at the end. 我有一个下拉列表,在列表末尾手动添加了一个项目。 I want to use some other color for this item, say red, to distinguish it from other items.
我想为此商品使用其他颜色,例如红色,以将其与其他商品区分开。 I can do this part.
我可以做这部分。 However, when I select the item, the text in the drop down's selected item is not red.
但是,当我选择该项目时,下拉菜单中所选项目中的文本不是红色。 I am not sure if there is any way to access it in order to apply style to it.
我不确定是否有任何方法可以对其应用样式。
This is what I used (assuming the "value" of this last item in drop down is 0): 这就是我使用的(假设下拉菜单中最后一项的“值”为0):
ListItem li = ddl1.Items.FindByValue("0");
if (null != li)
{
li.Attributes.Add("style", "color:red");
}
the above works coloring last item red. 上面的作品将最后一项着色为红色。 The following didn't work when I select this last item and want the selected item to displayed in the same red color:
当我选择最后一个项目并希望所选项目以相同的红色显示时,以下内容不起作用:
ListItem li = ddl1.Items.FindByValue("0");
if (null != li)
{
li.Attributes.Add("style", "color:red");
if (li.Selected)
ddl1.SelectedItem.Attributes.Add("style", "color:red");
else
ddl1.SelectedItem.Attributes.Add("style", "color:black");
}
Neither did this (this actually colors all list items red if I select the last item): 也没有这样做(如果我选择了最后一项,实际上会将所有列表项都涂成红色):
ListItem li = ddl1.Items.FindByValue("0");
if (null != li)
{
li.Attributes.Add("style", "color:red");
ddl1.Style["color"] = "red";
}
to use a pure CSS way, add this CSS 要使用纯CSS方式,请添加此CSS
#ddl1>option:checked {
background-color: red;
}
and in your C#, you can add and select the new option. 在C#中,您可以添加并选择新选项。 Let me know if this needs to change course
让我知道这是否需要改变方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.