Avatar of troycomp
troycomp

asked on 

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
C#CSS

Avatar of undefined
Last Comment
Teggert
Avatar of Teggert
Teggert

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' />
Avatar of CCSOFlag
CCSOFlag
Flag of United States of America image

As an example:
<element name>.Style["width"] = "1000px";
Avatar of troycomp
troycomp

ASKER

@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.
Avatar of troycomp
troycomp

ASKER

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;
        }
Avatar of Teggert
Teggert

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'
Avatar of troycomp
troycomp

ASKER

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

Avatar of Teggert
Teggert

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);
Avatar of troycomp
troycomp

ASKER

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 :(
Avatar of troycomp
troycomp

ASKER

Yes its CSS.aspx not nyCSS.aspx. I just called it myCSS.aspx to be consistent with what you called it
ASKER CERTIFIED SOLUTION
Avatar of Teggert
Teggert

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of troycomp
troycomp

ASKER

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
Avatar of troycomp
troycomp

ASKER

Wonderful
Avatar of Teggert
Teggert

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.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo