2016-07-08 12 views
0

Ich möchte die Farben der Dropdown-Liste in meiner C# -Web-App ändern können, kann ich dies mit CSS tun, anstatt jedes Dropdown einzeln benennen zu müssen?Ändern der Hintergrundfarbe einer ASP DropDownList mit nur CSS

Mit anderen Worten, ich möchte, dies zu tun, um zu vermeiden,:

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

Antwort

0

Verwenden Sie die CssClass Eigenschaft des DropDownList und wenden Sie die Klasse.

<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> 

CSS:

.dropdownlist { 
    background-color: blue; 
} 

Eine weitere Option ist ein Style-Objekt zu erstellen, und dass in der Code-behind auf Ihre Kontrollen gelten.

// create your style here 
Style style = new Style(); 
style.BackColor = System.Drawing.Color.Aqua; 

// apply style to each one 
comboBox1.Style = style; 
comboBox2.Style = style; 
comboBox3.Style = style; 
0

Schleife durch alle Ihre Comboboxen und wenden Sie eine CSS-Klasse an.

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"); 
       } 

     } 

    } 
0

dies eine Probe

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style> 
     select { 
      background-color: red; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:DropDownList ID="ddlTest" runat="server"> 
      <asp:ListItem Value="1">One</asp:ListItem> 
      <asp:ListItem Value="2">Two</asp:ListItem> 
      <asp:ListItem Value="3">Three</asp:ListItem> 
     </asp:DropDownList> 
     <asp:DropDownList ID="ddlTest2" runat="server"> 
      <asp:ListItem Value="4">Four</asp:ListItem> 
      <asp:ListItem Value="5">Five</asp:ListItem> 
      <asp:ListItem Value="6">Six</asp:ListItem> 
     </asp:DropDownList> 
    </div> 
    </form> 
</body> 
</html> 
0

Da ein WebControl Kind Kontrollen enthalten können, müssen Sie es rekursiv sein:

 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); 
      } 
     } 

über

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