0

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

2
  • do you have a server control for the property you want to get the attribute from ? Commented Jul 3, 2016 at 16:20
  • @ Veverke : No, it's not runat server Commented Jul 3, 2016 at 16:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.