ASP.NET ListBox Does Not Disable

This is a post from the archives. I'm sure it was fixed at one point.

See more archived posts in the archives.

With ASP.NET 4, ListBoxes no longer disable. This appears to be a bug with ASP.NET 4

According to the ASP.NET 4 Breaking Changes whitepaper

Controls that are not designed for user input (for example, the Label control) no longer render the disabled="disabled" attribute if their Enabled property is set to false (or if they inherit this setting from a container control).

That's great for label controls etc, but a ListBox (select element with the multiple attribute) is designed for user input and should support the disabled attribute.

There are a couple of options to work around this issue.

  1. extend the ListBox and override the SupportsDisabledAttribute property:
public class DisabledSupportedListBox : ListBox {
    public override bool SupportsDisabledAttribute { get { return true; } }
}
  1. server side, manually add the disabled attribute:
ListBox.Attributes.Add("disabled", "disabled");
  1. client side, manually add the disabled attribute:
$(function () {
    $("select.aspNetDisabled").attr('disabled', 'disabled');
});

If you're already extending the ListBox, option 1 is definitely the best bet. Otherwise, go with option 2 and only use option 3 as a last resort.