简体   繁体   中英

How to change the text color of disabled Asp.net DropDownList

I searched for some time on this question and couldn't find a working answer anywhere.

I have an asp DropDownList that gets disabled and enabled based on whether the form is in view mode or not. The problem I was having is when the DropDownList.Enabled = false the text is hard to read(grey on lightgrey).

I solved the issue by passing the DropDownList to some methods.

public void DisableDDL(ref DropDownList DDL)
{
    DDL.BackColor = System.Drawing.Color.LightGray;
    foreach (ListItem i in DDL.Items)
    {
        if (i != DDL.SelectedItem)
        {
            i.Enabled = false;
        }
    }
}

public void EnableDDL(ref DropDownList DDL)
{
    DDL.BackColor = System.Drawing.Color.White;
    foreach (ListItem i in DDL.Items)
    {
        i.Enabled = true;                        
    }
}

Is there another way to do this? I tried using css but that didn't work.

<style>
.disabledStyle
{
    color: black;        
}
</style>

myDDl.CssClass = "disabledStyle";

您需要将样式应用于每个单独的ListItem,而不是应用于DropDownList本身

I have just put in a dropdownlist and put it to enabled false in the controller, then I found out that it has a class called "aspNetDisabled", I have tried to use CSS to change color on it, it works perfectly.

<style>
    .aspNetDisabled 
    {
        color: #FFF;
        background-color: #000;
    }
</style>

In the code, if you put the dropdownlist, "ddl.enabled = false", it will be like this:

<select name="DropDownList1" id="DropDownList1" disabled="disabled" class="aspNetDisabled"></select>

If the dropdownlists are surrounded by a div with a class, use the class to define the disabled ones:

<style>
    .MyCssClass[disabled] 
    {
        color: #FFF;
        background-color: #000;
    }
</style>

Or try

:disabled,[disabled]
{
    -ms-opacity: 0.5;
    opacity:0.5;
}
</style>

As said in here: http://forums.asp.net/t/2028164.aspx?IE+11+disabled+buttons+links+not+shown+as+greyed+out

There is no readonly property for the dropdownlist control. But you can move the focus to another control when it receives the focus and that will prevent it from being changed and leave the text black.

The simplest way to do that:

<style>
    [disabled] { /* Text and background colour, medium red on light yellow */
    color:#933;
    background-color:#ffc;
    }

    </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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