PassionateDeveloper
PassionateDeveloper

Reputation: 15158

How do change CSS Class from Code Behind?

i have a CSS Style in my .css class in which a color is set to Blue for hundret of Buttons.

My customer now wants a option for green buttons, saved by a database field, so I ask the field:

                 if (!String.IsNullOrEmpty(user.SetColorButtons) && user.SetColorButtons == "Grün")
            {
                 //Change Style
            }

Now how to change the css-file here?

Its very hard to not change the file becuase I have arount 300 buttons in this site in around 40 pages, would be the hell to change it in the code in every freaking code behind...

Upvotes: 0

Views: 1970

Answers (3)

Steve Wellens
Steve Wellens

Reputation: 20638

Here is one way:

protected void ButtonServer_Click1(object sender, EventArgs e)
{
    MyButton.Style[HtmlTextWriterStyle.BackgroundColor] = "red";
}

Upvotes: 0

Mister Epic
Mister Epic

Reputation: 16743

Use the Literal object in your <head> tag:

<asp:Literal runat="server" id="Csslink"></asp:Literal>

Then in your code behind you can do something like:

if (!String.IsNullOrEmpty(user.SetColorButtons) && user.SetColorButtons == "Grün"){
    CssLink.Text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"theme1.css\">";
}
else{
    CssLink.Text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"theme2.css\">";
}

Upvotes: 1

Shyju
Shyju

Reputation: 218942

Keep two versions of the css file like yourproduct-blue.css and yourproduct-green.css and when the page loads, check the db to determine which css file to be used. Then it is all about writing an if condition to load the corresponding css file.

Upvotes: 2

Related Questions