Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
like image 863
Jacob K Avatar asked Sep 14 '10 13:09

Jacob K


3 Answers

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.

like image 96
Jacob K Avatar answered Oct 16 '22 06:10

Jacob K


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");
like image 10
CLaff Avatar answered Oct 16 '22 07:10

CLaff


Write the following line in the .cs file

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

like image 8
Pulkit Avatar answered Oct 16 '22 06:10

Pulkit