Solved

CSS and Master pages in ASP.net 2.0

Posted on 2006-07-06
10
551 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Technology Partners: 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!

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

627 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