I need my CSS in the master page to get reflected in content page

P

pushpadant

Hi,

I have one master page which has some table inside one of the <td> i
have my contentplaceholder.

on the <head> tag of the master page i have specified link to a global
CSS (stylesheet.css) file.

On my content page i have a textbox inside the contentplaceholder.

What i want is my all content page to use the same CSS file
(stylesheet.css).

If i specify on the page load

//HtmlLink cssLink = new HtmlLink();
//cssLink.Href = "StyleSheet.css";
//cssLink.Attributes.Add("rel","stylesheet");
//cssLink.Attributes.Add("type", "text/css");
//Header.Controls.Add(cssLink);

It works fine.

But this i will have to include in all my content pages, so if in case
if i want any other stylesheet file to be global, i will have to modify
code in all my content pages.

Can anybody suggest how can i specify a css file in master and inherit
that in all my content pages.

Thnx
Pushpadant
 
N

neilmcguigan

why don't you just declare the style sheet in the head section of the
master page?

....
<head>
<LINK href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
....
 
J

John Prado

Neil wrote the way to do what you want but if your pages will be in
subfolders in your app you need to get the relative path of the css file:

<LINK href="<%=ResoveUrl("~stylesheet.css%>" rel="stylesheet"
type="text/css">

You can add page_load event in your master page too and make the link on
the fly.
 
P

pushpadant

I have put the following in the master page

<head>
<LINK href="stylesheet.css" rel="stylesheet" type="text/css">
</head>


But it will not reflect on the content page, because only the contents
in the
<asp:contentplaceholder
will be inherited on the content pages,

what now i do is add in the content page:

HtmlLink cssLink = new HtmlLink();
cssLink.Href = "StyleSheet.css";
cssLink.Attributes.Add("rel","stylesheet");
cssLink.Attributes.Add("type", "text/css");
Header.Controls.Add(cssLink);

but this i will have to write in all the content pages, is there any
way to avoid this code replication.

somehow content page cud get the link from Master page
 
P

Pushpadant

I have created a common class, it has a func addCSS(htmlheader header),
now i call this function from all my pages passing header of the page
as parameter..

I this case if i want to use some other css page, will have to change
only at one place in the class.

Thnx for ur reponses.

Pushpadant
 
N

neilmcguigan

wrong. look at the resultant HTML source. you will see your style sheet
reference in there, with the way i proposed.

cheers

neil
 
A

Alan Silver

(e-mail address removed) writes
Can anybody suggest how can i specify a css file in master and inherit
that in all my content pages.

Well, apart from the ways that others have suggested, which should all
work and be a lot easier than the way you have suggested, you could look
at using themes. If you specify a theme, and put the CSS file in the
appropriate theme folder, then the framework will include it in every
page for you. You don't need to do anything.

HTH
 
P

Pushpadant

@Neil..

You were absolutely right...i tried ur method at the very
begining.....but it did not work....that was the only reason i
posted...but now it did seem to wrk....
i really cant guess y....
will try using theme some time later after i've got a working model.

Thnx for the help....
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top