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

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

问题描述

,我不认为这是有可能把一个内联<风格>标记为ASP.NET服务器控件。风格最终没有呈现到HTML输出。即使是可能的,我敢肯定,这是不好的做法,以做到这一点。

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.

是否有可能做到这一点?我可以看到它正在快速原型,只是有1或2 CSS类应用非常有用。

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.

更新:

每乔纳森的要求,我要去发布code。但是,当我打开我的项目,并再次加载页面(只是踢),其正确运行。我的猜测是,它必须是与重启,当你运行一个网页Visual Studio的启动ASP.NET开发服务器。

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.

推荐答案

根据 www.w3schools。 COM

style元素进去头部分。如果您要包含一个样式表在您的网页,你应该使用定义样式表外,并链接到它&LT;联系方式&gt;

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

因此​​,这不是一个好主意,包括样式元素(如&LT;风格类型=文\\ CSS&GT;&LT; /风格&GT; 块)中控制。如果你能,它很可能在一些浏览器的效果,但它不会验证,是不好的做法。

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

VB.NET

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

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

但请记住,这将取代那些在样式属性的设置的任何现有样式。如果您在code尝试在多个地方设置样式,这可能是一个问题,所以是值得提防。

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.

使用CSS类将是preferable因为你可以将多个样式声明,并避免冗余和页膨胀。从的WebControl 衍生的所有控件都一的CssClass 的财产,你可以使用,但同样要小心​​,不要覆盖已适用于其他地方现有的类。

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天全站免登陆