-2

Hi I want to change a css class properties using c#, like background image, font size, font style, and font. there is a interface for user to change these values. once he save them i add them to db. once user login back i can retrieve them from db but problem is how to show them.

This is the structure of my master page

    <html>
    <head></head>
    <body>
    <div id="Wrapper" class="abc">
    </div>
    </body>
    </html>

what is the easy way to change values of abc class according to relevant user?

2
  • "Duplicate" : stackoverflow.com/questions/175381/… Commented Jul 31, 2013 at 4:28
  • 2
    @RameshRams's example is a good for dynamic css. Commented Jul 31, 2013 at 4:31

6 Answers 6

5

try this

client side code

 <div id="Wrapper" runat="server" class="abc">

Server side code

string className="YourClassName";//You can change the name on runtime

Wrapper.Attributes["class"] = className;
Sign up to request clarification or add additional context in comments.

Comments

0

Put it in head inline CSS. The second option is to generate CSS file per user and link it in head.

The first option is much easier to implement.

Comments

0

first change div to be runat="server"

 <div id="Wrapper" runat="server" class="abc">

and then in code behind you can change class like

Wrapper.Attributes["class"]="classname";

Comments

0

Make an external CSS file and write your required values that fetch from DB in proper CSS style format and link it to your page.

1 Comment

sounds nice any useful link about writing css file using C#?
0

If you are using Razor it's pretty neat:

<div id="Wrapper" class="@Model.UserSelectedCssClass">

This lets you control what goes in the class attributes values from your model, assuming your model has a property called UserSelectedCssClass.

2 Comments

The OP asked for MVC ?????
The OP failed to mention if they were using WebForms or MVC, so since others already answered it for WebForms I provided an answer for MVC as an alternative so all bases were covered.
0


    You want to do two things here.
    1) Change div class
    2) Add db value in that div

    First of all change your html to following:

<html>
    <head></head>
    <body>
          <div id="Wrapper" runat="server" class="abc">
           </div>
    </body>
</html>


For 1) Wrapper.Attributes["class"] = "Class name";
For 2) Wrapper.InnerHtml = Your DB value.

5 Comments

What you mean by Wrapper.InnerHtml = Your DB value.????? Can you explain it ?
You said that you retrieve your values from db. If you want to just display those values then Wrapper.InnerHtml = "your values" will display in Wrapper div. If you want to apply your db values style to this div then Wrapper.Attributes.CssStyle["background"] = "Image from you db" Wrapper.Attributes.CssStyle["font-size"] = "font size from db" etc. just replace css tag in CssStyle and apply your db values. It will show effect on div("Wrapper") style.
But the "OP' want to just change the class name only , Not a total div format .
Deleema Fernando want to change div style according to db values, which are inserted via a form.
You okay for my side . But where is the OP asked for "Add db value in the div "

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.