Dynamically loading a css file with Stylesheet.aspx

I have a client who needs to load dynamic stylesheets per each of their client's personal settings.  I have seen the following used...

<LINK REL="STYLESHEET" TYPE="text/css" HREF="Stylesheet.aspx>

My question is how do I build my Stylesheet.aspx page to become a style used by a calling page?  Her eis what I have that doesn't seem to work yet:

Test.aspx (Html view)
      <HEAD>
            <title id="MetaTitle">Dynamic Title</title>
            <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="C#" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <meta name="Keywords" runat="server" content="" id="MetaKeywords">
            <link rel="stylesheet" type="text/css" href="Stylesheet.aspx" />
      </HEAD>
...

<TABLE id="Table5" cellSpacing="0" cellPadding="0" width="760" border="0">
      <TR>
            <TD>
                  <P>footer</P>
                  <P>
                        <cc1:Label id="lblTestCss" runat="server" CssClass="test">Test CSS</cc1:Label></P>
            </TD>
      </TR>
</TABLE>

Stylesheet.aspx (Html view)
<%@ Page language="c#" Codebehind="Stylesheet.aspx.cs" AutoEventWireup="false" Inherits="Foo.Web.StyleSheet" %>

.test{
    font-size: <%= fontSize %>;
}


Stylesheet.aspx (Code-behind view)

protected string fontSize;

private void Page_Load(object sender, System.EventArgs e)
{
      this.fontSize = "20";
}

Am I missing something key here?

Thx!

daveokstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramesh SrinivasTechnical ConsultantCommented:
Hi,

Not sure about that method but, you could probably initially save the client's settings.

In your page which requires the styles, place a literal control in your <head> tag.

Then in codebehind, ( maybe in page_load or pre-render event), build  a string which will represent all the style attributes and then set the text property of the literal control to this string.

hope that helps,

regards,

KS
0
AGBrownCommented:
I wonder if the css link you have seen refers to an HttpHandler, not an actual aspx page. Your aspx page will probably be returning the wrong headers, and won't be interpreted as a css page.

If you replace your style with something like
.body {
      background-color:#ff0000;
}
do you get a red background? If you open Stylesheet.aspx you will see the text you've printed.
0
AGBrownCommented:
Sorry, managed to submit before I'd finished. Have a play with Fiddler (available on the web) to check the headers that are coming back from your page and css files. Css files should return Content-type=text/css. I'll bet your page is returning something like Content-Type: text/html; charset=utf-8. You can change this in your Page_Load with the Response.ContentType property.

Andy
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AGBrownCommented:
Dave,

It looks like it is the headers. Note that the style I posted above should have been
body {
     background-color:#ff0000;
}

and not

.body {
     background-color:#ff0000;
}

If you add the lines
            private void Page_Load(object sender, System.EventArgs e)
            {
                  Response.ContentType = "text/css";
                  Response.ContentEncoding = System.Text.Encoding.UTF8;
            }
into your code-behind, it should work.

Andy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daveokstAuthor Commented:
Response.ContentType = "text/css";
Response.ContentEncoding = System.Text.Encoding.UTF8;

Excellent... the above settings were what I was missing.  After testing the ouput of Stylesheet.aspx I was able to hewn in on the correct syntax... now I can have all my styles in one sheet, set the dynamic ones via the page variables based on a clientId I pass in the <link rel="stylesheet" type="text/css" href="Stylesheet.aspx" id="link" runat="server" />, which with a HttpGenericControl I can set it to end up like <link rel="stylesheet" type="text/css" href="Stylesheet.aspx?clientId=foo"/>.

Much thanks!
0
AGBrownCommented:
Nice, glad it work.

You could also maybe not pass the clientId in the query string, as the cookie sent with the request should just let you get the clientId from the session, that way you don't have to put your database ids out in the html.

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.