简体   繁体   English

使用CSS更改ASP.NET中dropdownlist所选项目的背景颜色

[英]Change background colour of dropdownlist selected item in ASP.NET with CSS

I would like to change the background colour of the selected item in dropdownlists, listboxes, and textboxes. 我想在下拉列表,列表框和文本框中更改所选项目的背景颜色。 Is this possible with CSS? 这可能与CSS有关吗?

Thanks 谢谢

UPDATE UPDATE

Just tried this CSS: 刚试过这个CSS:

::-moz-selection 
{
    background-color:#8CCA1E;
    color:Black;
}

Works great for selected text but not for hover colours or selected item colours on dropdownlists or listboxes. 适用于所选文本,但不适用于下拉列表或列表框中的悬停颜色或选定项目颜色。

You can set background colors from the .aspx page by specifying either the CssClass, or the BackColor property.. it looks like: 您可以通过指定CssClass或BackColor属性从.aspx页面设置背景颜色。它看起来像:

<asp:ListBox CssClass="MyListBox" BackColor="#e0e0e0"></asp:ListBox>

Setting the selected item is a little trickier... I don't believe there is an attribute for this directly. 设置所选项目有点棘手......我不相信这是直接的属性。 You can set it in javascript, or jQuery, something like: 您可以在javascript或jQuery中设置它,例如:

// Never mind, this won't work
<script type="text/javascript">
  $(document).ready(function() {
    $('#MyListBox').click(function() {
        $("#MyListBox option:selected").css("background-color", "#e0e0e0");
    }); 
  });
</script>

I'll test that to make sure it works... if you need a straight javascript solution, I'll see what I can come up with. 我将测试它以确保它的工作...如果你需要一个直接的JavaScript解决方案,我会看到我能想到的。 But if you're not using jQuery, and you want fine-grained control over your controls, now is a good time to learn it : ) 但是,如果您不使用jQuery,并且希望对控件进行细粒度控制,那么现在是学习它的好时机:)

Thanks to Justin for corrections to the script! 感谢Justin对剧本的更正!


EDIT 编辑
Hover is easy... add a style: 悬停很容易......添加一种风格:

  select > option:hover {
    background-color:  #ffffd0;
  }

That will set the background color on any listbox item you hover over on the page. 这将在您悬停在页面上的任何列表框项目上设置背景颜色。 jQuery can be used to set the click function on all listboxes, not just specifically #MyListBox... give me a couple of minutes, I'll add some code jQuery可以用来设置所有列表框上的click函数,而不仅仅是#MyListBox ...给我几分钟,我会添加一些代码


UPDATE UPDATE
After extensive research, exhausting every resource I had or can put my hands on, the best answer I come up with is: Setting the selected color can't be done. 经过广泛的研究,耗尽了我所拥有的每一种资源或者可以把手放在上面,我提出的最佳答案是:无法设置所选的颜色。

Not even the jQuery code I came up with will work. 甚至我想出的jQuery代码都不会起作用。 The selected color is set by the OS, and can't be overridden. 所选颜色由OS设置,不能被覆盖。

Sorry, best I can do is the :hover pseudo-selector 对不起,我能做的最好的是:hover伪选择器
: ( :(

.

You could create CSS rules for this and apply them programmatically in the change handlers in your code. 您可以为此创建CSS规则,并以编程方式在代码中的更改处理程序中应用它们。 Or you could tap into the System.Drawing namespace to achieve the same thing in your handlers. 或者您可以使用System.Drawing命名空间来在处理程序中实现相同的功能。

// CSS 
.dropDownBackColor { background-color:#00ff00; }

// Code-Behind: in your selected index changed event handler 
// for the drop down list
ddl.CssClass = "dropDownBackColor";

OR 要么

ddl.BackColor = System.Drawing.Color.Yellow;

Another approach is using JS.. 另一种方法是使用JS ..

<asp:DropDownList ID="DropDownList1" runat="server" onchange="SelectedItemCLR(this);">  
   <asp:ListItem Text="Name1" Value="1"></asp:ListItem>  
   <asp:ListItem Text="Name2" Value="2"></asp:ListItem>  
   <asp:ListItem Text="Name3" Value="3"></asp:ListItem>  

<script type="text/javascript">  
  function SelectedItemCLR(source) {   

      for (var i = 0; i < source.options.length; i++) {   
        if (i != source.selectedIndex)   
              source.options[i].style.backgroundColor = "White";   
          else   
              source.options[i].style.backgroundColor = "Red"; 

      }   
  }           

You might want to try out the css selector ::selection which sounds like it will do what you want. 你可能想尝试一下css selector :: selection,它听起来会像你想做的那样。 Here's a good article on it. 这是一篇很好的文章。 The only problem is it seems to be css3 only, so you may want to try the jquery solution James B posted, depending on what your browser requirements are. 唯一的问题是它似乎只是css3,所以你可能想尝试James B发布的jquery解决方案,具体取决于你的浏览器要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM