• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Modify or Create a CSS class in C#

I have an aspx page with a ListView on it being populated from a database. A dropdown is at the top of the page and the user can select which client to view. Each client has different colors for text and borders and such. I have a stylesheet for every cleint, but in the css classes, I'm only changing 3 things and the rest stays the same.

 .grid
        {
            border: solid 2px #000; /** this changes **/
            font-family: Tahoma;
            font-size: 12px;
            width: 700px;
            background-color: #fff;
            margin: 0px auto;
            position: relative;
            margin-top: 50px;
            padding-bottom: 5px;
        }
        .grid H2
        {
            background: #8988a5 url(../images/sgtitle-bg.gif); /** this changes **/
            border-bottom: solid 1px #000;                              /** this changes **/
            height: 28px;
            line-height: 28px;
            margin: 0px;
            padding: 0px 0px 0px 5px;
            color: #000;
            font-size: 15px;
        }

We have over 40 clients coming and it's ridiculous to have over 40 style sheets when only 3 lines are changing.  How do I modify or create css classes in C#? This is .NET 4.0
0
troycomp
Asked:
troycomp
  • 7
  • 5
1 Solution
 
TeggertCommented:
Probably the best thing to do is create an aspx page with or without a codebehind file, then just do your css manipulation there. Here's a basic example.

<%@ Page Language="C#" %>
<%
    string color = "blue";
%>
H1 { background-color:<%= color %>; }


Then reference that file in your other pages just like you would any other css file.

<link type='text/css' rel='stylesheet' href='../myCSS.aspx' />
0
 
CCSOFlagCommented:
As an example:
<element name>.Style["width"] = "1000px";
0
 
troycompAuthor Commented:
@Teggert

I Like your suggestion, but I cant get my page to recognize the css in an aspx page

<link type='text/css' rel='stylesheet' href='../myCSS.aspx' />

I added this to my page, but no styling appears.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
troycompAuthor Commented:
My myCSS.aspx looks like this:

<%@ Page Language="C#"%>


<%@ Page Language="C#" %>
<%
   string color = "#123456";
%>


      /* Remove margins from the 'html' and 'body' tags, and ensure the page takes up full screen height */
html, body {height:100%; margin:0; padding:0;}
/* Set the position and dimensions of the background image. */
#page-background {position:fixed; top:0; left:0; width:100%; height:100%;}
/* Specify the position and layering for the content that needs to appear in front of the background image. Must have a higher z-index value than the background image. Also add some padding to compensate for removing the margin from the 'html' and 'body' tags. */
#content {position:relative; z-index:1;padding:10px;}
       
        .grid
        {
            border: solid 2px <%= color %>;
            font-family: Tahoma;
            font-size: 12px;
            width: 700px;
            background-color: #fff;
            margin: 0px auto;
            position: relative;
            margin-top: 50px;
            padding-bottom: 5px;
        }
        .grid H2
        {
            background: #8988a5 url(../images/sgtitle-bg.gif);
            border-bottom: solid 1px #000;
            height: 28px;
            line-height: 28px;
            margin: 0px;
            padding: 0px 0px 0px 5px;
            color: #000;
            font-size: 15px;
        }
0
 
TeggertCommented:
Just make sure the location of your myCSS.aspx file is correctly referenced. If it's in your site root, just do '/myCSS.aspx' or 'myCSS.aspx' instead of '../myCSS.aspx'
0
 
troycompAuthor Commented:
Its at the root. Im adding it dynamically, like i was doing for the other stylesheets:

           var link = new HtmlLink();
            link.Attributes.Add("href", Page.ResolveClientUrl("myCSS.aspx"));
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            head.Controls.Add(link);

0
 
TeggertCommented:
Give this a try.

            var link = new HtmlLink();
            link.Attributes.Add("href", Page.ResolveClientUrl("~/myCSS.aspx"));
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            Header.Controls.Add(link);
0
 
troycompAuthor Commented:
I tried that first, no luck. I even put the link directly on the page. No styling. My old cole looked like this:

link.Attributes.Add("href", Page.ResolveClientUrl("~/Styles/" + site + "Style.css"));

where the variable site was the client the user selected. This worked fine. I moved myCSS.aspx under the styles folder and updated my code

link.Attributes.Add("href", Page.ResolveClientUrl("~/Styles/CSS.aspx"));

No styling :(
0
 
troycompAuthor Commented:
Yes its CSS.aspx not nyCSS.aspx. I just called it myCSS.aspx to be consistent with what you called it
0
 
TeggertCommented:
Roger that. The only thing I see odd is that your CSS code example had two page directives. I assume that was just a paste typo?
0
 
troycompAuthor Commented:
That fixed it!!!! Man you're a genius. I had no idea you could use an aspx page as a stylesheet. Thank you so much! 500 points for you
0
 
troycompAuthor Commented:
Wonderful
0
 
TeggertCommented:
No problem man. Glad it worked out. Feel free to post any other problems you run into here and I'll help out as I have time.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now