Solved

CSS and Master pages in ASP.net 2.0

Posted on 2006-07-06
10
546 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why don't I see this table in EDMX file? 2 23
Problem to page 4 74
Registering DLL 5 43
Getting multiple values from a dataset ASP.NET(VB) 3 37
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 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

15 Experts available now in Live!

Get 1:1 Help Now