0

I want to add background color to this drop down.. how can I do this.. I tried adding new{style="background-color: #fff;"} to the below code.. but this does not seem to working..

     <%=Html.DropDownList("dd", 
                       new List<SelectListItem>
         {
            new SelectListItem{ Text="dd1", Value = "dd1" }, 
            new SelectListItem{ Text="dd2", Value = "dd2" }

         }
                            , new { onchange = "jsfunc()" } 


         )%>

I tried:

     <%=Html.DropDownList("dd", 
                       new List<SelectListItem>
         {
            new SelectListItem{ Text="dd1", Value = "dd1" }, 
            new SelectListItem{ Text="dd2", Value = "dd2" }

         }
                            , new { onchange = "jsfunc()" }
                            , new {style="background-color: #fff;"}


         )%>

2 Answers 2

3

You should avoid using inline styles. I suggest creating a class and the syntax would be

new { @class = "yourclass", @onchange = "jsfunc()" }

Sign up to request clarification or add additional context in comments.

2 Comments

This does not work.. I replaced my last line of code with yours and it does not seem to recognize the fourth argument for html.dropdownlist..
This is likely because you have ` , new { onchange = "jsfunc()" } ` try ` new { @onchange = "jsfunc()", @class = "yourclass" } ` However, I suggest that you use external javascript as Null mentioned in his answer.
1

I think you had a syntax error, new {@style = "background-color: #fff"}, but you should use a class instead.

Edit

new { @onchange = "jsfunc()", @style = "background-color: #fff"}

8 Comments

problem is it does not seem to recognize the fourth argument for html.dropdownlist.. even if I use class instead of style.. I get error.
See my edit, does that resolve it? You can pass one or more attributes.
This does not change the background color immediately. I have to click on the drop down to see the color? why is that
Could you post the rendered HTML?
<h1><select id="dd" name="dd" onchange="jsfunc()" style="background-color: gray"><option value="dd1">dd1</option> <option value="dd2">dd2</option> </select></h1>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.