如何让用户控件了解 ASP.NET 中的 css 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34390/
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
How to make user controls know about css classes in ASP.NET
提问by Serhat Ozgel
Since there are no header sections for user controls in asp.net, user controls have no way of knowing about stylesheet files. So css classes in the user controls are not recognized by visual studio and produces warnings. How can I make a user control know that it will relate to a css class, so if it is warning me about a non-existing css class, it means that the class really do not exist?
由于asp.net 中没有用户控件的标题部分,因此用户控件无法了解样式表文件。因此,visual studio 无法识别用户控件中的 css 类并产生警告。如何让用户控件知道它将与 css 类相关,所以如果它警告我不存在的 css 类,这意味着该类确实不存在?
Edit: Or should I go for a different design like exposing css classes as properties like "HeaderStyle-CssClass" of GridView?
编辑:或者我应该采用不同的设计,例如将 css 类公开为 GridView 的“HeaderStyle-CssClass”等属性?
回答by Adam Lassek
Here's what I did:
这是我所做的:
<link rel="Stylesheet" type="text/css" href="Stylesheet.css" id="style" runat="server" visible="false" />
It fools Visual Studio into thinking you've added a stylesheet to the page but it doesn't get rendered.
它使 Visual Studio 误以为您已向页面添加了样式表,但它没有被呈现。
Here's an even more concise way to do this with multiple references;
这是使用多个引用执行此操作的更简洁的方法;
<% if (false) { %>
<link rel="Stylesheet" type="text/css" href="Stylesheet.css" />
<script type="text/javascript" src="js/jquery-1.2.6.js" />
<% } %>
As seen in this blog postfrom Phil Haack.
正如Phil Haack 的这篇博文中所见。
回答by Abdul Saboor
Add the style on your usercontrol and import css in it.
在用户控件上添加样式并在其中导入 css。
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WCReportCalendar.ascx.vb"
Inherits="Intra.WCReportCalender" %>
<style type='text/css'>
@import url("path of file.css");
// This is how i used jqueryui css
@import url("http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css");
</style>
your html
回答by Rob Cooper
If you are creating composite UserControl, then you can set the CSSClassproperty on the child controls..
如果要创建复合用户控件,则可以在子控件上设置CSSClass属性。
If not, then you need to expose properties that are either of the Styletype, or (as I often do) string properties that apply CSS at the render type (i.e. take them properties and add a styleattribute to the HTML tags when rendering).
如果没有,那么您需要公开样式类型的属性,或者(正如我经常做的那样)在渲染类型中应用 CSS 的字符串属性(即在渲染时获取它们的属性并为 HTML 标签添加样式属性) .
回答by Kirtish Srivastava
You Can use CSS
direct in userControl
.
您可以CSS
直接在userControl
.
Use this in UserControl
:
在UserControl
:
<head>
<title></title>
<style type="text/css">
.wrapper {
margin: 0 auto -142px;
/* the bottom margin is the negative value of the footer's height */
}
</style>
</head>
This will work.
这将起作用。