仅使用CSS更改ASP DropDownList的背景颜色

时间:2016-07-08 18:44:03

标签: c# css asp.net dropdownbox

我希望能够在我的C#网络应用中更改下拉列表背景颜色,我可以使用CSS执行此操作,而不必单独为每个下拉列表命名吗?

换句话说,我想避免这样做:

private void Form_Load(object sender, EventArgs e)
{
    comboBox1.BackColor = Color.Aqua;
    comboBox2.BackColor = Color.Aqua;
    comboBox3.BackColor = Color.Aqua;
    etc
    .
    .
    .
} 

5 个答案:

答案 0 :(得分:0)

使用CssClass的{​​{1}}属性并应用该课程。

DropDownList

CSS:

<asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropdownlist"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="dropdownlist"></asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" CssClass="dropdownlist"></asp:DropDownList>

另一种选择是创建一个.dropdownlist { background-color: blue; } 对象并将其应用于后面代码中的控件。

Style

答案 1 :(得分:0)

遍历所有组合框并应用css类。

    public void foo(List<Control> foundSofar, Control parent) 
    {

        foreach(var c in parent.Controls) 
        {
              if(c is ComboBox) //Or whatever that is you checking for 
              {
                     c.Attributes.Add("style", "color: aqua");
              }

        }  

    }

答案 2 :(得分:0)

这是一个样本

if (document.addEventListener) {
  document.addEventListener('contextmenu', function(e) {
    alert("You've tried to open context menu"); //here you draw your own menu
    e.preventDefault();
  }, false);
} else {
  document.attachEvent('oncontextmenu', function() {
    alert("You've tried to open context menu");
    window.event.returnValue = false;
  });
}

答案 3 :(得分:0)

由于WebControl可以包含子控件,因此您必须对其进行递归:

        private void SetDropDownListBackGround(IEnumerable controls)
        {
            foreach (WebControl control in controls)
            {
                var list = control as DropDownList;
                if (list != null)
                {
                    list.BackColor = Color.Aqua;
                }

               SetDropDownListBackGround(control.Controls);
            }
        }

通过

调用
private void Form_Load(object sender, EventArgs e)
{
    SetDropDownListBackGround(Page.Controls); //or whatever container
    ...
}

答案 4 :(得分:0)

我来晚了,但是这是一篇不错的文章,介绍了如何使用background-color来更改CSS和其他属性,甚至是一个带有两个圆角的Dropdown的示例而另外两个线性:

来源:https://parallelcodes.com/asp-net-dropdownlist-css-style/

CSS

.mydropdownlist1
{
color: #fff;
font-size: 20px;
padding: 5px 10px;
border-radius: 5px 12px;
background-color: #292929;
font-weight: bold;
}

ASPX

<asp:DropDownList runat="server" ID="ddlItems" CssClass="mydropdownlist">
<asp:ListItem Text="One" Value="One" />
<asp:ListItem Text="Two" Value="Two" />
<asp:ListItem Text="Three" Value="Three" />
<asp:ListItem Text="Four" Value="Four" />
<asp:ListItem Text="Five" Value="Five" />
<asp:ListItem Text="Six" Value="Six" />
<asp:ListItem Text="Seven" Value="Seven" />
<asp:ListItem Text="Eight" Value="Eight" />
<asp:ListItem Text="Nine" Value="Nine" />
<asp:ListItem Text="Ten" Value="Ten" />
</asp:DropDownList>