将内联 CSS 应用于 ASP.NET 服务器控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/92427/
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-29 19:57:03  来源:igfitidea点击:

Apply inline CSS to an ASP.NET server control

asp.netcss

提问by Jason Z

Based on a simple test I ran, I don't think it's possible to put an inline <style> tag into an ASP.NET server control. The style did not end up rendering to the output HTML. Even if it was possible, I'm sure it is bad practice to do this.

根据我运行的一个简单测试,我认为不可能将内联 <style> 标记放入 ASP.NET 服务器控件中。样式最终没有渲染到输出 HTML。即使有可能,我也确信这样做是不好的做法。

Is it possible to do this? I can see it being useful for quick prototypes that just have 1 or 2 CSS classes to apply.

是否有可能做到这一点?我可以看到它对于只需要应用 1 或 2 个 CSS 类的快速原型很有用。

Update:

更新:

Per Jonathan's request, I was going to post the code. But, when I opened my project and loaded the page again (just for kicks) it ran correctly. My guess is that it had something to do with restarting the ASP.NET development server that Visual Studio launches when you run a page.

根据乔纳森的要求,我打算发布代码。但是,当我打开我的项目并再次加载页面(只是为了踢球)时,它运行正常。我的猜测是它与重新启动 Visual Studio 在您运行页面时启动的 ASP.NET 开发服务器有关。

In any case, when I included identical multiple controls on the page, I got multiple identical styles as well. This would probably be the explanation why doing this is a bad thing. Regardless, it is always good to know best practices and alternative methods of accomplishing a task, so I do thank everyone for their answers.

无论如何,当我在页面上包含相同的多个控件时,我也会得到多个相同的样式。这可能就是为什么这样做是一件坏事的解释。无论如何,了解完成任务的最佳实践和替代方法总是好的,所以我非常感谢大家的回答。

采纳答案by Tom Robinson

According to www.w3schools.com:

根据www.w3schools.com

The style element goes in the head section. If you want to include a style sheet in your page, you should define the style sheet externally, and link to it using <link>.

style 元素位于 head 部分。如果要在页面中包含样式表,应在外部定义样式表,并使用<link>.

So it's not a good idea to include style elements (e.g. a <style type="text\css"></style>block) in a control. If you could, it'd probably have an effect in some browsers but it wouldn't validate and is bad practice.

因此,<style type="text\css"></style>在控件中包含样式元素(例如块)并不是一个好主意。如果可以,它可能会在某些浏览器中产生影响,但它不会验证并且是不好的做法。

If you want to apply styles inline to an element then either of these would work:

如果您想将样式内联应用于元素,那么以下任一方法都可以:

C#

C#

myControl.Attributes["style"] = "color:red";

myControl.Attributes.Add("style", "color:red");

VB.NET

网络

myControl.Attributes("style") = "color:red";

myControl.Attributes.Add("style", "color:red");

But bear in mind that this will replace any existing styles that are set on the style attribute. This may be a problem if you try setting styles in more than one place in the code so is something to watch out for.

但请记住,这将替换在 style 属性上设置的任何现有样式。如果您尝试在代码中的多个位置设置样式,这可能是一个问题,因此需要注意。

Using CSS classes would be preferable as you can group multiple style declarations and avoid redundancy and page bloat. All controls derived from WebControlhave a CssClassproperty which you can use, but again be careful not to overwrite existing classes that have been applied elsewhere.

使用 CSS 类会更可取,因为您可以将多个样式声明分组并避免冗余和页面膨胀。从WebControl派生的所有控件都有一个您可以使用的CssClass属性,但再次注意不要覆盖已在其他地方应用的现有类。

回答by NakedBrunch

Intellisense won't give you hints but you can do this:

Intellisense 不会给你提示,但你可以这样做:

<asp:Label ID="Label1" runat="server" Text="Label" style="color:Red;"></asp:Label>

回答by Hutch

If you use Attributes["style"], you are overwriting the style each time you call it. This can be an issue if you are making the call in two different sections of code. As well, it can be an issue because the framework includes properties for basic settings like border and colour that also will be applied as inline styles. Here is an example:

如果您使用 Attributes["style"],则每次调用它时都会覆盖该样式。如果您在两个不同的代码部分进行调用,这可能是一个问题。同样,这也可能是一个问题,因为框架包含基本设置的属性,如边框和颜色,这些属性也将作为内联样式应用。下面是一个例子:

// wrong: first style will be overwritten
myControl.Attributes["style"] = "text-align:center";
// in some other section of code
myControl.Attributes["style"] = "width:100%";

To play nicely, set styles like this instead:

为了更好地演奏,请设置如下样式:

// correct: both style settings are applied
myControl.Attributes.CssStyle.Add("text-align", "center");
// in some other section of code
myControl.Attributes.CssStyle.Add("width", "100%");

回答by Vaibhav

I think you will have to add it as an attribute to the server control... for it to render to HTML.

我认为您必须将其作为属性添加到服务器控件...才能呈现为 HTML。

So basically (in C#),

所以基本上(在 C# 中),

ControlName.Attributes["style"] = "color:red";