将内联CSS应用于ASP.NET服务器控件 [英] Apply inline CSS to an ASP.NET server control

查看:202
本文介绍了将内联CSS应用于ASP.NET服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于一个简单的测试,我跑了,我不认为可以放一个内联< style>标记到ASP.NET服务器控件中。样式没有最终呈现到输出HTML。即使是可能的,我敢肯定这是不好的做法。



这是否可以做到这一点?



更新:

$ b,我可以看到它对于快速原型只需要1或2个CSS类就可以应用。
$ b

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



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

解决方案

根据 www.w3schools.com


样式元素位于头部。如果要在页面中包含样式表,则应在外部定义样式表,并使用< link> 链接到样式表。


因此,包含样式元素不是一个好主意(例如< style type =text\css> / style> block)。如果可以,它可能会在一些浏览器的效果,但它不会验证,是不好的做法。



如果你想应用样式内联元素那么这两个都可以工作:



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);但是请注意,这将替换在样式属性上设置的任何现有样式。 。这可能是一个问题,如果你尝试设置样式在多个地方的代码,所以是值得注意的。



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


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.

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.

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.

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.

解决方案

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

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.

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

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.

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

这篇关于将内联CSS应用于ASP.NET服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆