Jacob K
Jacob K

Reputation: 549

ASP.NET C# ListBox server control will not disable

I have 4 server side ListBox controls. All of them have their Enabled property set to false, yet when rendered they are definitely enabled. They are all multiple select. These have no data binding or any code behind touching them. Below is the markup for all of them (save the ID). I am running v4 of the .NET Framework with IIS6.

<asp:ListBox runat="server" ID="lstProduct" Enabled="false" SelectionMode="Multiple" Rows="6"></asp:ListBox>

Here is the markup that is generated by the runtime:

<select size="6" name="ctl00$ctl00$MainContent$MainContent$lstProduct" multiple="multiple" id="MainContent_MainContent_lstProduct" class="aspNetDisabled">

Upvotes: 13

Views: 23008

Answers (8)

Will N
Will N

Reputation: 1610

I had the same problem but with CheckBoxList.

Setting its Enabled property to false didn't disable it. The panel it was inside of would also not have an effect on it when Enabled = false.

The solution was to use a foreach loop over the items in the CheckBoxList.

foreach (var item in checkBoxList.Items.Cast<ListItem>())
{
    item.Enabled = false;
}

Upvotes: 1

franzo
franzo

Reputation: 1469

You can use a little jquery as a bandaid until this is properly fixed. If you put this somewhere that's run for all pages it will fix it for all disabled listboxes on all pages:

$(document).ready(function () {
    $("select.aspNetDisabled").attr('disabled', 'disabled');
});

Upvotes: 2

Pulkit
Pulkit

Reputation: 101

Write the following line in the .cs file

ListBox.Attributes.Add("disabled", "true");

Upvotes: 10

franzo
franzo

Reputation: 1469

This should be considered a bug in the .Net Framework.

http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770141 says:

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

Also see rationale for the change (rendering valid html) at http://msdn.microsoft.com/en-us/library/system.web.ui.control.renderingcompatibility.aspx.

But a list box is designed for user input and the disbled attribute is supported in html, so it ought to render disabled="disabled".

Upvotes: 2

Alex KeySmith
Alex KeySmith

Reputation: 17101

You may wish to instead disable the options within the select box, as this will allow scrolling.

//Listbox cannot be disabled directly, instead the inners should be disabled instead.
foreach(ListItem item in lbCategory.Items)
{
    item.Attributes.Add("disabled", "disabled");

    if (item.Selected)
    {
        //cannot reliably style with [disabled='disabled'][selected='selected'] or :checked:selected etc, so need a class
        item.Attributes.Add("class", "disabledSelected"); 
    }
}

I then use the following CSS, so the user can still see preselected items.

/* Slightly lighter colour than the normal #3399FF because you cannot change the foreground color in IE, so means that it isn't contrasted enough */
select option.disabledSelected { background-color: #97cbff !important} 

Unfortunately from my initial investigations it's a bit of a pain to style disabled input elements in a nice cross browser way. I've setteled with using a class for my purposes, however this article regarding styling disabled form elements might help.

You may also notice that in IE, click events will still be triggered, which seemed to deselect the options but only in some combinations of trying to use [disabled='disabled'][selected='selected'] or :checked:selected etc.

Upvotes: 1

Pavel2lety
Pavel2lety

Reputation: 61

A better solution is to inherit from the ListBox class and then override the SupportsDisabledAttribute property. Detailed information can be found in MSDN library

e.g.

public class MyListBox : ListBox
{
   public override bool SupportsDisabledAttribute { get { return true; } }
}

Upvotes: 6

Jacob K
Jacob K

Reputation: 549

I found a solution. In the <system.web> section of web.config, you must add <pages controlRenderingCompatibilityVersion="3.5">.

With Asp.net 4.0, any control that does not take specific user input (textbox or password), will not be rendered with a disabled="disabled" attribute when Control.Enabled = false is set.

Upvotes: 16

CLaff
CLaff

Reputation: 316

Try this:

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
   this.lstProduct.Attributes.Add("disabled", "");
  }
}

To remove it you can just remove the disabled tag like this:

this.lstProduct.Attributes.Remove("disabled");

Upvotes: 13

Related Questions