-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?

mr.soroush
  • 1,110
  • 2
  • 14
  • 31
  • "Duplicate" : http://stackoverflow.com/questions/175381/how-to-edit-css-style-of-a-div-using-c-sharp-in-net –  Jul 31 '13 at 04:28
  • 2
    @RameshRams's example is a good for dynamic css. – Janki Jul 31 '13 at 04:31

6 Answers6

4

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;
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.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
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";
Manoj Purohit
  • 3,413
  • 1
  • 15
  • 17
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.

Usman
  • 3,200
  • 3
  • 28
  • 47
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.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • The OP asked for MVC ????? –  Jul 31 '13 at 05:10
  • 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. – Timothy Walters Aug 06 '13 at 02:47
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.
Harshil Raval
  • 2,115
  • 1
  • 13
  • 10
  • What you mean by Wrapper.InnerHtml = Your DB value.????? Can you explain it ? –  Jul 31 '13 at 05:11
  • 1
    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. – Harshil Raval Jul 31 '13 at 06:04
  • But the "OP' want to just change the class name only , Not a total div format . –  Jul 31 '13 at 06:07
  • Deleema Fernando want to change div style according to db values, which are inserted via a form. – Harshil Raval Jul 31 '13 at 06:12
  • You okay for my side . But where is the OP asked for "Add db value in the div " –  Jul 31 '13 at 06:16