-3

I'm working with asp.net and have a dropdown list that is populated by an sql query. I want certain items to be styled differently if they have previously been selected, any thoughts? The table I'm working with has a flag for having been selected.

2
  • 1
    do you have any code? Jun 10, 2014 at 21:14
  • What view engine are you using? razor 2? razor 3? aspx? What have you tried so far?
    – andyw_
    Jun 10, 2014 at 21:16

3 Answers 3

1

when building your select, make sure that you mark the previously selected options with "selected" and then in your CSS you would have

.mySelectClass option {
    background-color: #FFF;
}

.mySelectClass option:checked {
    background-color: #00F;
    color: #FFF;
}

the :checked works on selects the same as checkboxes.

0

You can style your options like <option value="volvo" style="background-color: #dddddd">Volvo</option>;

    <select class="auto">
      <option value="saab">Saab</option>
      <option value="volvo" style="background-color: #dddddd">Volvo</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>

Demo

4
  • -1 Your background-color is lost when "Volvo" is selected. Check your fiddle if you want. Jun 10, 2014 at 21:20
  • No really. I couldn't reproduce. Jun 10, 2014 at 21:23
  • Just see here->jsfiddle.net/WBE4C/1. All I have done is updated the background-color to red. When selected background-color is not set. The color is visible only when the dropdown is expanded. Jun 10, 2014 at 21:26
  • It's not happening for me man. I'm using Chrome v35. Beside the question doesn't have any demo code, only asked how to style previously selected option which is saved in db. But anyway you can restyle the option on change event. Jun 10, 2014 at 21:40
0

Since you did not provide ANY code, here is one approach which loops through all the items in the drop-downl-list after it has been populated and checkes to see if it can find any items that have the value "selected", and change its background color.

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    foreach (ListItem myItem in DropDownList1.Items)
    {
        //Do some things to determine the color of the item
        //Set the item background-color like so:

        if(myItem.Value.Equals("Selected")) // check if the value is 'selected'
            myItem.Attributes.Add("style", "background-color:red");
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.