简体   繁体   English

样式下拉列表中选定的项目

[英]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.

相关问题 下拉列表选择项 - Drop Down List Selected Item 在第一个下拉列表中选择该项目时,如何从第二个下拉列表中删除该项目? - How to remove an item from the second drop down list when that item is selected in the first drop down list? 如何在下拉列表中获取所选项目的值? - How to get the value of the selected Item in a Drop down list? 使用 on change 事件将下拉列表的选定项目链接到 sql 脚本 - Link the selected item of drop down list to sql script with an on change event 有没有更好的方法在下拉列表中设置所选项? - Is there a better way to set the selected item in a drop down list? 没有将所选的下拉列表项返回给控制器 - Not getting the selected drop down list item back to controller 将下拉列表中的所选项目放入文本框中 - Place selected item from drop down list in to a text box 获取网格行命令事件上的下拉列表选择项 - Get the drop down list selected item on grid row command event 下拉列表中的模型绑定不返回所选项目的ID - model binding in drop down list does not returning the id of selected item 如何获得下拉列表中所选项目的价值? - how to get value of a selected Item in drop down list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM