Link to home
Start Free TrialLog in
Avatar of sunny-j
sunny-j

asked on

CSS and Master pages in ASP.net 2.0

Hello,

I searched high and low for an answer to this question without much luck.

What code do i need to ensure that the css works in child pages in master pages in ASP.net 2.0

I have put this in between the <head> tags in the master page.
<link href="stylesheet.css" rel="stylesheet" type="text/css" />

but it does not seem to apply within the content holders in the child pages.
Any ideas on how to get this right. thanks.
ASKER CERTIFIED SOLUTION
Avatar of rohanbairat3
rohanbairat3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are your child pages and your master page in different folders?  Try using an href that is relative to the root folder.  For example, if your stylesheet is in a folder called "style", which is in the root folder, your link would look like this...

<link href="/styles/stylesheet.css" rel="stylesheet" type="text/css" />

Notice the extra slash ( / ) at the beginning.  That tells it to start looking for the file path in the root folder of the website.  The other thing you can try is to make .net form the file path for you...

<link rel="StyleSheet" href="<%=ResolveUrl("~/styles/stylesheet.css")%>" type="Text/Css" />

I hope this helps!
Bah! just a minute too late. :)
Avatar of rohanbairat3
rohanbairat3

:)
It happens ...

Another approach will be to use the base tag in the server that solves many linking issues in the application:

<html>
<head>
<base href="http://www.mywebsite.com">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
</body>
</html>

Note that if you use the base tag and you have your asp.net web forms in sub folders, then you need to use the following code at the bottom of the page:

<script language="javascript">
document.yourFormName.action = window.location;
</script>

A lengthy approach but it really help sometimes :)

-Nauman.
Interesting... I've never seen the base tag before.  

The only potential problem that I see with Nauman's solution above is that any user that has javascript turned off (whether by choice or not), probably will not be able to browse your site properly.

If you're developing for an environment where you know everyone will have javascript, then there's not problem.  If not, then you'll potentially be turning away visitors to your site.

Just something to keep in mind...
With ASP.NET, javascript is a must otherwise none of the postback event will fire for any of the server side control. The web application must check in advance if javascript is enabled or not to make sure that things will run smoothly :)

-Nauman.
It is true that if you're not careful about how you build your site, then it is entirely possible that your site will force users to have javascript enabled.  However, if you're careful about it, you can create a very full featured ASP.Net website that is perfectly accessible to everybody, including those without javascript.

Only some of the ASP.Net controls require the user to have javascript enabled.  And some of those only require it if you use certain features.  For example, if you use the AutoPostback feature on any control, then the user will be required to have javascript.  AutoPostback is not required for a fully functional website, though.  In fact, I might argue that it's a bad idea from a usability standpoint, but that's another discussion for another time.  :)

Here's the MSDN page explaining the accessibility of each of the ASP.Net controls...

http://msdn2.microsoft.com/en-us/library/ms227996.aspx

It's a great read for anyone that is concerned about accessibility on their ASP.Net websites.
Another way to reference it from the root of your site is using the ~ sign.

For example:

<link href="~/stylesheet.css" rel="stylesheet" type="text/css" /> No matter what directory you're in, this should work.

Regards,
Max.
The only problem with using the tilde "~" is that I'm pretty sure that it only works if the control is being run on the server side (runat="server").

That's why in my previous example above, I had to use ResolveURL...

<link rel="StyleSheet" href="<%=ResolveUrl("~/styles/stylesheet.css")%>" type="Text/Css" />

As far as I know if you don't make the server interpret the tilde, then the browser will have no idea what to do with it.  (someone please let me know if I'm wrong about that)