Applying stylesheet styles to master page

S

sasquatch

Hi,

I've a a site with nested master pages and content pages. I tried using
a theme with a stylesheet in the app_themes directory referencing it in
the web.config file from a pages tag theme attribute. This works for
the content pages, but it doesn't seem able to apply the styles to the
top master page even though I did set the master head tag to runat
server. Is this by design? If so, what is the best way to apply styles
to the top master page content? What did work for me was to add a style
sheet link in the master page header but this doesn't seem to be a very
elegant solution to me; my goal is to have site wide styles applied to
all masters and content pages from a single stylesheet. Is applying
styles to the header programatically from the content pages code behind
a better answer? If so, an example would be much appreciated!

Thanks for any help on this!
 
W

Wouter van Vugt

Hi,

to dynamically add a stylesheet from a MasterPage, use the following
code inside the OnLoad for instance:

HtmlLink link = new HtmlLink();
link.Href = "MyStylesheet.css"
link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString(),"stylesheet");
Page.Header.Controls.Add(link);

Your other questions I do not know about.
Grtz, Wouter
 
B

Brock Allen

The master and the page get merged and rendered as a single page into the
browser, so I'm not sure why using a CSS from App_Themes isn't working for
you -- try looking at the rendered HTML source and perhaps there's something
wrong with the CSS class you're using in the master?
 
C

clintonG

Here's a cleaner (to read) implementation ...

protected void Page_Init(object sender, EventArgs e)
{
// Your comments here...
HtmlLink link = new HtmlLink();
link.Href = "~/StyleSheet.css";
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(link);
}

Note the use of Page_Init emits the declaration 'before' the stylesheet
declaration generated by the Theme. If you want or need a stylesheet to
over-ride styles used by the Theme put the code shown above into the
Page_Load event.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 
C

clintonG

I got started writing all of my style declarations in the theme.css file
located in the App_Themes folders. One great big collection of declarations
in a single file. A bad habit it seems and perhaps a misunderstanding of how
other aspects of Themes actually work because using a monolithic file
provides no mechanism for browser dependent styles so I became motivated to
write a separate style sheet into the head element.

<%= Clinton Gallagher
 
J

Juan T. Llibre

Unfortunately, that throws an error for me at :

Page.Header.Controls.Add(link);
 
S

Scott Allen

Unfortunately, that throws an error for me at :

Page.Header.Controls.Add(link);

I have a guess at what is happening:

You'll need a <head> element with runat="server", otherwise you'll see
a NullReferenceException.
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top