Solved

CSS and Master pages in ASP.net 2.0

Posted on 2006-07-06
10
550 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:sunny-j
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
rohanbairat3 earned 500 total points
ID: 17053895
Try to create a simple function


Try to provide proper location for the style sheet.... reference it from root

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

if its in root try using "/stylesheet.css"


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

or if its in css folder



0
 
LVL 6

Expert Comment

by:TheKyle
ID: 17053904
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!
0
 
LVL 6

Expert Comment

by:TheKyle
ID: 17053928
Bah! just a minute too late. :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:rohanbairat3
ID: 17053952
:)
It happens ...

0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17054573
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.
0
 
LVL 6

Expert Comment

by:TheKyle
ID: 17054632
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...
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17054705
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.
0
 
LVL 6

Expert Comment

by:TheKyle
ID: 17054777
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.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 17056565
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.
0
 
LVL 6

Expert Comment

by:TheKyle
ID: 17058779
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)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question