2016-07-03 48 views
0

在我的.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();} 
} 

我如何设置从后面的代码动态背景颜色的值?

感谢

+0

你有属性的服务器控件,你想获得属性? – Veverke

+0

@ Veverke:不,它不是runat服务器 – ron

回答

1

如果这是一个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"所以你可以直接从后端分配属性。

问候

0

您可以从一个样式属性添加到您的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"属性:

​​