【问题标题】:code-behind/stylesheet bridge in ASP.NET? add a style to a css selector in code-behind in ASP.NET?ASP.NET 中的代码隐藏/样式表桥?在 ASP.NET 的代码隐藏中向 CSS 选择器添加样式?
【发布时间】:2012-09-19 16:27:40
【问题描述】:

我需要知道 ASP.NET 服务器端代码是否可以引用在给定页面上有效的样式表。是否有可能在 ASP.NET 代码隐藏 中获取对样式表中现有 CSS 选择器的引用?在服务器端代码隐藏中是否可能出现以下伪代码?

     MyPage.Stylesheets(0)("DIV#foo").Attributes("display").Value = "none"


    NOTE:  Assume that <div id = 'foo'>lorem ipse...</div> has been injected
    into the DOM by javascript. There is no server side counterpart to the DIV.
    It was **not** part of the ASP.NET markup:  <div id="foo" runat="server"....

同样,服务器端,而不是 jQuery/javascript 客户端。

我正在尝试根据某些服务器端条件使特定的 DOM 元素不可见,但该元素是在客户端实例化的。我无法从服务器端代码中获得对它的引用,但也许我可以引用样式表中的选择器?

谢谢

【问题讨论】:

  • 这并不容易,除非它已经 runat="server" 到正在渲染的元素并将它们从服务器添加到 DOM。直接解析 CSS 不值得花费额外的处理,因为这真的会减慢您的应用程序的速度。

标签: asp.net stylesheet bridge


【解决方案1】:

实际上,虽然没有使用我预期的方法,但我找到了一种方法来实现这一点。 Visual Studio 不喜欢以下内容,但它可以工作:

  <style type="text/css" media="all">DIV#foo{<%= FooCSS %>}</style>

在代码隐藏中:

         Public Readonly Property FooCSS as String
           Get
             Return "display:none"
           End Get
         End Property

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 2011-07-08
    • 2014-08-12
    • 2011-12-19
    相关资源
    最近更新 更多