Where is the “aspNetDisabled” class defined and wh

2019-04-04 02:52发布

问题:

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

回答1:

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?



回答2:

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)



回答3:

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


回答4:

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.



回答5:

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.



回答6:

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.

2) Select Build. 3) Change Target Framework.



回答7:

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


回答8:

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