By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

Applying stylesheet styles to master page

P: n/a
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!

Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.To String(),"stylesheet");
Page.Header.Controls.Add(link);

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

Nov 20 '05 #2

P: n/a
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?

-Brock
DevelopMentor
http://staff.develop.com/ballen
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!

Nov 21 '05 #3

P: n/a
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/

"Wouter van Vugt" <wo*****@infosupport.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.To String(),"stylesheet");
Page.Header.Controls.Add(link);

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

Nov 21 '05 #4

P: n/a
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
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
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?

-Brock
DevelopMentor
http://staff.develop.com/ballen
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!


Nov 21 '05 #5

P: n/a
Unfortunately, that throws an error for me at :

Page.Header.Controls.Add(link);
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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/

"Wouter van Vugt" <wo*****@infosupport.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.To String(),"stylesheet");
Page.Header.Controls.Add(link);

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


Nov 21 '05 #6

P: n/a
On Mon, 21 Nov 2005 13:52:05 -0400, "Juan T. Llibre"
<no***********@nowhere.com> wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 21 '05 #7

P: n/a
You nailed that one, Scott.
Thanks.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:56********************************@4ax.com...
On Mon, 21 Nov 2005 13:52:05 -0400, "Juan T. Llibre"
<no***********@nowhere.com> wrote:
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.

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.