简体   繁体   English

样式化服务器控件生成的html

[英]styling html generated by server controls

Just starting to do web development in .NET. 刚开始在.NET中进行Web开发。 I apologize if this question is simplistic. 如果这个问题过于简单,我深表歉意。 If I have a server control perhaps: 如果我有服务器控件,也许:

<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                    <AnonymousTemplate>
                        <asp:LinkButton ID="header_su_btn" runat="server" CssClass="su_btn">Sign Up</asp:LinkButton><br />
                        <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a>
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
                    </LoggedInTemplate>
                </asp:LoginView>

Is there any way to go about styling the server control with inline styling or is the only way to specify the css class of the generated element and then do the styling in a seperate style sheet? 是否有任何方法可以使用内联样式对服务器控件进行样式设置,还是指定生成元素的css类然后在单独的样式表中进行样式设置的唯一方法?

Thanks in advance. 提前致谢。

It will depend on what's the top-level (superclass) of the whole control. 这将取决于整个控件的顶级(超类)。 If it's WebControl , you'll be forced to define your CSS stylesheet in some file. 如果是WebControl ,则将被迫在某些文件中定义CSS样式表。 In the other hand, if it's a HtmlGenericControl you would be able to add a style attribute with inline CSS styles: 另一方面,如果它是HtmlGenericControl ,则可以使用内联CSS样式添加style属性:

<div style="color: #f0f0f0" runat="server" />

Anyway, I'm not sure if you're going in the right route if you want to style elements using style attribute in modern Web development. 无论如何,如果您要在现代Web开发中使用style属性来设置元素的style ,我不确定您是否走了正确的道路。 You should avoid that as soon as possible and get the job done right. 您应该尽快避免这种情况,并正确完成工作。 A separate file is worth the effort. 一个单独的文件是值得的。

Can you expand on why inline styles are bad? 您能解释为什么内联样式不好吗?

Browser Wars: A new hope 浏览器大战: 新希望

In a galaxy, far far away, in the 20th century AC in the far Earth planet, it took place the Browser Wars . 在遥远的银河系中,发生在20世纪AC遥远地球行星上,发生了“ 浏览器战争” After some fights, where each other wanted to rule the Web , software vendors arrived to a great conclusion: we need to standarize Web development . 经过一番争斗,彼此都想统治Web ,软件供应商得出了一个很好的结论: 我们需要使Web开发标准化

blah, blah, blah, blah 等等,等等,等等,等等

[...] Thus, they tried to fix Web development, and HTML would be the document definition/layout, CSS the style and JavaScript the code . [...]因此,他们试图修复Web开发,HTML将是文档定义/布局,CSS 是样式 ,JavaScript 是代码

Summary: coding styles in HTML is mixing two different concepts. 简介:HTML中的编码样式混合了两个不同的概念。 Document structure and the style, and this is why using style attribute is a bad practice . 文档结构和样式,这就是为什么使用style属性是一种不好的做法

It's still there because of backwards compatibility with old-days' (90s) Web pages. 由于与旧版(90年代)网页的向后兼容性,它仍然存在。

TL;DR: Because separation of concerns is a good idea . TL; DR:因为关注点分离是个好主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM