[英]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.