【问题标题】:How to make user controls know about css classes in ASP.NET如何让用户控件了解 ASP.NET 中的 css 类
【发布时间】:2010-09-07 06:09:57
【问题描述】:

由于在 asp.net 中没有用于用户控件的标题部分,因此用户控件无法了解样式表文件。因此,Visual Studio 无法识别用户控件中的 CSS 类并产生警告。如何让用户控件知道它将与一个 css 类相关,所以如果它警告我一个不存在的 css 类,这意味着该类真的不存在?

编辑:或者我应该采用不同的设计,例如将 css 类公开为 GridView 的“HeaderStyle-CssClass”等属性?

【问题讨论】:

    标签: asp.net css visual-studio-2008 user-controls


    【解决方案1】:

    在你的用户控件上添加样式并在其中导入 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 
    

    【讨论】:

      【解决方案2】:

      您可以在userControl 中直接使用CSS

      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>
      

      这会起作用。

      【讨论】:

      • 这意味着您必须在每个 ascx 文件中复制 CSS,这在很多方面都很糟糕。
      • 不可以在MasterFile中使用这个用户控件,那么就不用在每个文件中都写了
      • 您还可以在用户控件中附加样式表,如下所示。
      • 是的,您可以在 MasterPage 中放置对 UserControl 的引用,但问题是如何让 UserControl 识别 CSS 规则存在于单独的 .css 文件中。
      • 您还可以在用户控件中附加样式表,如下所示。
      【解决方案3】:

      这就是我所做的:

      <link rel="Stylesheet" type="text/css" href="Stylesheet.css" id="style" runat="server" visible="false" />
      

      它使 Visual Studio 误以为您已经向页面添加了样式表,但它没有被渲染。


      这里有一个更简洁的方法来处理多个引用;

      <% if (false) { %>
          <link rel="Stylesheet" type="text/css" href="Stylesheet.css" />
          <script type="text/javascript" src="js/jquery-1.2.6.js" />
      <% } %>
      

      Phil Haack 在this blog post 中看到。

      【讨论】:

      • @blachawk if (false) 意味着它永远不会被执行,代码在那里纯粹是对VS的提示。
      【解决方案4】:

      如果您正在创建复合用户控件,那么您可以在子控件上设置CSSClass 属性..

      如果没有,那么您需要公开Style 类型的属性,或者(就像我经常做的那样)在渲染类型上应用 CSS 的字符串属性(即获取它们的属性并添加 样式 渲染时 HTML 标签的属性)。

      【讨论】:

      猜你喜欢
      • 2011-05-18
      • 2012-02-14
      • 2010-10-22
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 2010-10-19
      相关资源
      最近更新 更多