在我的 .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();}
}
如何从后面的代码中动态设置背景颜色的值?
谢谢
最佳答案
如果这是 aspx,您可以尝试将该类的成员定义为 protected 成员:
protected 字符串 _myServerColor;
然后在页面加载时分配该 Prop :
protected void Page_Load(对象发送者,EventArgs e)
{
_myServerColor = "#FFF";//将其分配给您的数据库颜色
}
然后,只要您的样式标签在同一页面内,您就可以:
<style type="text/css">
.item .item_background .item_general_info .button_wrapper .add_button {
background-color: "<%= _myServerColor %>";
}
</style>
最简洁的方法是让这个控件 runat="server"
这样你就可以直接从后端分配属性。
问候
关于c# - 如何从代码隐藏属性中获取样式属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171867/