18

When I set the "Disabled" property of an ASP.NET TextBox control to false, the final rendered HTML textarea tag (sent to the browser) includes an 'class="aspNetDisabled"' attribute in addition to the 'disabled="disabled"' attribute. Where is the "aspNetDisabled" class defined?

It seems to me that it's not defined anywhere, and the real killer is that this useless class is interfering with my defined classes, because ASP.NET is rendering this into the control as a duplicate CSS class attribute:

<textarea [...] disabled="disabled" class="aspNetDisabled" class="boxsizingBorder largeinput">

Can anyone else confirm this bug?


Additional Info

IIS Version: 7.0.6000.16386
AppPool .NET Framework Version: v4.0
Server control tag in ASPX page:

<asp:TextBox ID="txtInput1" class="boxsizingBorder largeinput" runat="server" TextMode="MultiLine"></asp:TextBox>.
Triynko
  • 18,766
  • 21
  • 107
  • 173
  • 1
    What version ASP.NET are you using? Can you post your original markup and code? – IrishChieftain Apr 04 '11 at 19:52
  • 4
    For readers, the complete answer is in comments of the marked answer. The proper ASP.NET attribute to use in ASPX markup is "CssClass". The "class" attribute would be considered a custom attribute, and although it's valid to use it and it is the final attribute rendered to the HTML, the ASP.NET rendering engine will fail to merge it with it's own rendered version of the "class" attribute value. – Triynko Apr 04 '11 at 20:27
  • Maybe use inheritance and/or tag mapping? http://leedumond.com/blog/fixing-asp-net-server-control-rendering-issues-with-tag-mapping/ – IrishChieftain Apr 04 '11 at 23:16

8 Answers8

18

For anyone that might still be looking for this, we can define this css class during Application_Start in the Global.asax:

void Application_Start(object sender, EventArgs e)
{
    WebControl.DisabledCssClass = "customDisabledClassName";
}

Source: WebControl.DisabledCssClass Property (MSDN)

Timo
  • 226
  • 3
  • 6
5

I ended up doing the following, which effectively removes the insertion of the extra class for disabled items.

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    WebControl.DisabledCssClass = "";
}
Austin Salgat
  • 416
  • 5
  • 13
3

That is because of controlRenderingCompatibilityVersion. If your framework version is above 4, then this property will be set default to "pages controlRenderingCompatibilityVersion="4.0"

Change the controlRenderingCompatibilityVersion="3.5" and you can see class="aspNetDisabled" will be removed from html.

2

You might want to look at this:

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltextarea.aspx

For one, there is no "class" attribute. This is a HTML control; if you want server-side access, you need to add the runat="server" attribute. There is a "Disabled" property. There is also a "Style" property.

Can you explain exactly what it is you are trying to do and why you're not using a TextBox instead with the TextMode property set to multiline?

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 1
    I am already using a TextBox web control (System.Web.UI.WebControls.TextBox). The runat="server" attribute is set in the ASPX source file. I am using the Disabled property (setting it to false) in the ASPX.CS server-side code file. I was describing the HTML that ASP.NET renders and serves to the browser for the TextBox web control (FireFox -> View Page Source). The final HTML that gets served includes a duplicate class attribute that was overriding mine. A work-around is to set the static TextBox.DisabledCssClass to an empty string, so it will stop rendering the duplicate class attribute. – Triynko Apr 04 '11 at 19:36
  • Did you try setting Visible to false instead? – IrishChieftain Apr 04 '11 at 19:50
  • I want to disable the control, not hide it and break the layout of the page. Setting visible to false will result in the control not being rendered to the output stream at all, which is not my intention. I'm really just concerned with what's triggering ASP.NET to render a duplicate class attribute. – Triynko Apr 04 '11 at 19:55
  • 11
    Try CssClass instead of class. I got it working correctly in FX4 – IrishChieftain Apr 04 '11 at 19:58
  • 1
    Ok. So the bug is that while custom attributes on tags are allowed, and are propagated to the final rendered HTML, the rendering engine is not taking into consideration the case where a custom tag is specified that is also a rendered tag, and it fails to merge the values. This is probably overlooked for performance reasons, but IMO to correctly support custom tags as it claims to do, it should handle the general case where a custom attribute is specified that matches an output attribute by attempting to merge them into a single attribute value or at least have well-defined precedence rules. – Triynko Apr 04 '11 at 20:23
  • 1
    Therefore, it's probably best to stick with the CssClass attribute specific to ASP.NET control to keep the attribute rendering process as pure as possible, since using custom attributes can interfere as demonstrated. – Triynko Apr 04 '11 at 20:24
1

Be careful, in .net 4.5 the generated html has changed :

disabled="disabled"

Will not always be present, so use "aspNetDisabled" or the defined DisabledCssClass for javascript or css.

J-P
  • 11
  • 2
1

It may have Visual Studio Framework Problem. Solution is that you have to select your Framework in which you made your project. For changing framework follow the Step.

1) Right Click on Particular Project -> Property Pages. enter image description here

2) Select Build. enter image description here 3) Change Target Framework.

user3405179
  • 184
  • 11
1

When you use Enabled="false" for any control in asp.net it automatically binds css class "aspNetDisabled" with that control. If you don't want to use the class provided automatically you can achieve this using below line of code at the very first step of you .cs file.

System.Web.UI.WebControls.TextBox.DisabledCssClass = "CLASS_NAME_WHICH_YOU WANT_TO_APPLY";

// Or you can make it blank // In above line of code I have done this for TextBox control. If you want to apply for other controls you can do this as well like:-

System.Web.UI.WebControls.CheckBox.DisabledCssClass = "CLASS_NAME_WHICH_YOU WANT_TO_APPLY";
Amit Joshi
  • 57
  • 3
0

at first look it seems that this is by (bad) design

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.supportsdisabledattribute.aspx

If the SupportsDisabledAttribute property of a control is false and the control is disabled, ASP.NET sets the class attribute of the rendered HTML element to the value of the WebControl.DisabledCssClass property. The default value of the WebControl.DisabledCssClass property is "aspNetDisabled". To provide a disabled appearance for disabled controls, you must define a CSS rule for the class that is represented by the value of the WebControl.DisabledCssClass property. The HTML element that is rendered for a control might have more than one value in its class attribute if there is a value in its CssClass property. For more information, see the DisabledCssClass property.

it is strange that I see both disabled="disabled" and class="aspNetDisabled" in the same webpage: https://www.dropbox.com/s/sv47x7yoqdzkzh4/Screenshot%202016-10-24%2018.24.16.png?dl=0 I have a panel there that is disabled and it seems to add disabled="disabled" to all its rendered children (including DropDownList ones), except for the ListBox ones that I happen to have explicitly set to Enabled="False", which seem to get class="aspNetDisabled". When I enable the parent panel, those listboxes when rendered still use class="aspNetDisabled" (instead of disabled="disabled" as DropDownList seems to use) and user can select an item in them (they're not disabled).

So it does look like a bug at ListBox control, probably it sets "SupportsDisabledAttribute" to false while DropDownList must be setting it to true. If it is so, this is silly, since they both end up rendered as "select", the ListBox one just using "size=4" to show 4 items by default

George Birbilis
  • 2,782
  • 2
  • 33
  • 35
  • rule of least surprise broken – George Birbilis Oct 24 '16 at 15:03
  • don't mind much about non-disabled look, although it would be nice if it was consistent with other controls. Initially started investigating it cause those ListBoxes showed up as DropDownLists in Android/iOS, but then found the real cause of that - seems WebKit is not respecting "size" attribute of "select" in HTML on mobile devices: http://forums.htmlhelp.com/index.php?showtopic=19066 - that issue occurs irrespectively of disabled state most probably, but when I tried to check it as a workarround, came accross the issue where both aspNetDisabled" and disabled="disabled" exist in the same page – George Birbilis Oct 25 '16 at 10:29