开发者

ASP.NET C# ListBox server control will not disable

开发者 https://www.devze.com 2023-01-16 15:36 出处:网络
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. Thes开发者_如何学编程e have no

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. Thes开发者_如何学编程e 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">


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.


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");


Write the following line in the .cs file

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


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; } }
}


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


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');
});


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消