Solved

CSS and Master pages in ASP.net 2.0

Posted on 2006-07-06
10
544 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now