Embedding CSS with inline code

S

Sean

Is it possible to embed a css file and use it as webresource when it's having
some code inside? An example would be:

..Container
{
background-color : <% =BackgroundColor %>;
}

when "BackgroundColor" is a property in my custom server control?

Thank you.
 
P

Phillip Williams

Hi Sean,

No, it is not possible because the Style tag is not a server control that
can runat=â€serverâ€. The Head can runat=â€server’ though. Technically you can
try something like this in the page markup:

<HEAD id="PageHeadSection" runat="server">
</HEAD>

Then in the codebehind:

Protected PageHeadSection As HtmlGenericControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim StyleTag As New HtmlGenericControl
styletag.InnerHtml = "<style>.Container{Background-color:" +
strBackgroundColor + "}</style>"
PageHeadSection.Controls.Add(styletag)

End Sub

Though this is technically possible, I prefer (in terms of separating the
styling from the code) to add the style names dynamically to the server
controls but keep the style definitions static. For example you can create
all the possible styles for the container:

..ContainerRed{/* definitinon*/}
..ContainerBlue{/* definitinon*/}

Then in the codebehind assign the appropriate container style to the objects
cssClass property.
 
P

Phillip Williams

Hi Sean,

Thanks for posting this link for such an interesting article. I will spend
some time reading it.

The information I gave you earlier applies to ASP.NET1.1. The Style tag in
ASP.NET2.0 however can indeed runat="server" and therefore I managed to do
something like this:

<style runat="server" id="StyleTag"></style>

and in the codebehind, I tried this and it worked:

StyleTag.InnerText += ".Container{background-color:red;"};

But using your original expression in ASP.NET 2.0 brought up interesting
issues. I got first an error pointing to the partial class that is generated
by ASP.NET for the webpage about a missing semicolon. When I added this the
code ran but I got an empty value (instead of the value "Red"). When I
stepped through the code it seems that the @__RenderStyleTag (which assigns
the codebehind value) runs after the Page_PreRenderComplete and the value did
not appear on the browser.
This is an interesting issue. I will keep looking into it and perhaps some
one from Microsoft would comment also.
 
S

Sean

Hi Phillip,

I was thinking maybe WebResource can run on embedded resources and inject
code that is related to the entire assembly, but not to a specific instance
of a class that resides in that assembly? This is only my speculation, and
I'm not so sure about it. Would be nice to have someone from Microsoft to
respond. And thank you for helping with the issue.

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,348
Latest member
nethues

Latest Threads

Top