繁体   English   中英

如何从属性背后的代码获取样式属性的值?

[英]How to get value of style attribute from a code behind property?

在我的.aspx中,我有这个:

 <style type="text/css">
       .item .item_background .item_general_info .button_wrapper .add_button {

           background-color: /* MyProp from code behind */
       }

   </style>

在后面的代码上:

public String MyProp 
{
  get {return DB.GetColor();}
}

如何从后面的代码动态设置background-color的值?

谢谢

如果这是一个aspx,则可以尝试将类上的该成员定义为受保护的成员:

protected string _myServerColor;

然后在页面加载时分配该道具:

protected void Page_Load(object sender, EventArgs e) { _myServerColor = "#FFF"; // assign this to your db color }

然后,只要您的样式标签在同一页面内,您就可以执行以下操作:

<style type="text/css">
       .item .item_background .item_general_info .button_wrapper .add_button {

           background-color: "<%= _myServerColor %>";
       }

   </style>

最干净的方法是使此控件运行runat="server"以便您可以直接从后端分配属性。

问候

您可以通过以下方式从代码背后将样式属性添加到CSS样式类:

Style style1 = new Style();
style1.BackColor = Color.Orange; // Insert the desired color here
Header.StyleSheet.CreateStyleRule(style1, null, ".item .item_background .item_general_info .button_wrapper .add_button");

为了使其正常工作,页面的head必须具有runat="server"属性:

<head runat="server">
    <style type="text/css">
        .item .item_background .item_general_info .button_wrapper .add_button 
        {
            ...
        }
    </style>
    ...
</head>

暂无
暂无

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

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