“aspNetDisabled”类在哪里定义,为什么 ASP.NET 会为它呈现干扰重复的 CSS“类”属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5542998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:07:44  来源:igfitidea点击:

Where is the "aspNetDisabled" class defined and why does ASP.NET render an interfering duplicate CSS "class" attribute for it?

asp.netcsscontrols

提问by Triynko

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?

当我将 ASP.NET TextBox 控件的“Disabled”属性设置为 false 时,最终呈现的 HTML textarea 标记(发送到浏览器)除了“disabled="disabled”属性外还包括一个“class="aspNetDisabled”属性' 属性。“aspNetDisabled”类在哪里定义?

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:

在我看来,它没有在任何地方定义,真正的杀手是这个无用的类干扰了我定义的类,因为 ASP.NET 将它作为重复的 CSS 类属性呈现到控件中:

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

IIS 版本:7.0.6000.16386
AppPool .NET Framework 版本:v4.0
ASPX 页面中的服务器控制标记:

<asp:TextBox ID="txtInput1" class="boxsizingBorder largeinput" runat="server" TextMode="MultiLine"></asp:TextBox>.

采纳答案by IrishChieftain

You might want to look at this:

你可能想看看这个:

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

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.

一方面,没有“类”属性。这是一个 HTML 控件;如果需要服务器端访问,则需要添加 runat="server" 属性。有一个“禁用”属性。还有一个“样式”属性。

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

你能准确地解释一下你想要做什么,为什么你不使用 TextBox 而是将TextMode属性设置为多行?

回答by Timo

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

对于可能仍在寻找此内容的任何人,我们可以在 Global.asax 中的 Application_Start 期间定义此 css 类:

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

Source: WebControl.DisabledCssClass Property (MSDN)

来源:WebControl.DisabledCssClass 属性 (MSDN)

回答by Austin Salgat

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

回答by Pyari Gopal

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

那是因为 controlRenderingCompatibilityVersion。如果您的框架版本高于 4,则此属性将默认设置为“pages controlRenderingCompatibilityVersion="4.0"

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

更改 controlRenderingCompatibilityVersion="3.5",您可以看到 class="aspNetDisabled" 将从 html 中删除。

回答by J-P

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

请注意,在 .net 4.5 中,生成的 html 已更改:

disabled="disabled"

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

不会总是存在,因此使用“aspNetDisabled”或为 javascript 或 css 定义的 DisabledCssClass。

回答by user3405179

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.

它可能有 Visual Studio框架问题。解决方案是您必须选择您制作项目的框架。对于更改框架,请遵循步骤。

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

1) 右键单击​​特定项目 -> 属性页。 在此处输入图片说明

2) Select Build. enter image description here3) Change Target Framework.

2) 选择构建。 在此处输入图片说明3) 改变目标框架。

回答by Amit Joshi

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.

当您对 asp.net 中的任何控件使用 Enabled="false" 时,它会自动将 css 类“aspNetDisabled”与该控件绑定。如果您不想使用自动提供的类,您可以在 .cs 文件的第一步使用下面的代码行来实现这一点。

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

// 或者您可以将其设为空白 // 在上面的代码行中,我为 TextBox 控件完成了此操作。如果您想申请其他控制,您也可以这样做:-

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

回答by George Birbilis

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

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.

如果控件的 SupportsDisabledAttribute 属性为 false 并且控件被禁用,则 ASP.NET 将呈现的 HTML 元素的类属性设置为 WebControl.DisabledCssClass 属性的值。WebControl.DisabledCssClass 属性的默认值为“aspNetDisabled”。要为禁用的控件提供禁用的外观,您必须为由 WebControl.DisabledCssClass 属性的值表示的类定义 CSS 规则。如果在其 CssClass 属性中有一个值,则为控件呈现的 HTML 元素在其 class 属性中可能有多个值。有关详细信息,请参阅 DisabledCssClass 属性。

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=0I 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).

奇怪的是,我在同一个网页中看到了 disabled="disabled" 和 class="aspNetDisabled":https://www.dropbox.com/s/sv47x7yoqdzkzh4/Screenshot%202016-10-24%2018.24.16.png ?dl=0我有一个禁用的面板,它似乎将 disabled="disabled" 添加到所有呈现的子项(包括 DropDownList 的),除了我碰巧明确设置为 Enabled="False 的 ListBox ",这似乎得到 class="aspNetDisabled"。当我启用父面板时,这些列表框在呈现时仍然使用 class="aspNetDisabled"(而不是 DropDownList 似乎使用的 disabled="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

所以它看起来确实像 ListBox 控件的一个错误,可能它把“SupportsDisabledAttribute”设置为 false 而 DropDownList 必须将它设置为 true。如果是这样,这是愚蠢的,因为它们最终都呈现为“选择”,默认情况下,ListBox 仅使用“size = 4”显示 4 个项目