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



在我的。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属性:

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>

最新更新