简体   繁体   中英

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

In my .aspx I have this :

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

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

   </style>

On the code behind :

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

How can I set the value of the background-color dynamically from the code behind ?

Thanks

If this is a aspx, you can try to define that member on the class as a protected member:

protected string _myServerColor;

Then assign that prop when the page loads:

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

And then, as long as your style tag is within the same page, you could do:

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

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

   </style>

Cleanest way would be to make this controls runat="server" so you could assign properties from the backend directly.

Regards

You can add a style attribute to your CSS style class from code-behind this way:

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");

In order for that to work, the head section of the page must have the runat="server" attribute:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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