Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 1st, 2006, 11:45 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add dynamic css to master/content page proj-How?

Help anyone please:

I have a 30 page website under development where all pages derive from 1 masterpage. Using a master page precludes using the <head> tag where I could specify a <link> to an external css page - a separate one for each aspx page.

Making a big internal <style> section on the master page defeats the efficiency of a load-one-time external css page, so that's out too.

Boils down to: How to dynamically add a <link> tag to a masterpage when each aspx page loads. Anyone got an idea on how I can do that? Or some equivalent functionality? Thanks.

VictorVictor

Reply With Quote
  #2 (permalink)  
Old June 1st, 2006, 04:06 PM
Imar's Avatar
Wrox Author
Points: 67,082, Level: 100
Points: 67,082, Level: 100 Points: 67,082, Level: 100 Points: 67,082, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,200
Thanks: 65
Thanked 1,404 Times in 1,384 Posts
Default

Hi VictorVictor,

Take a look at this thread:

http://p2p.wrox.com/topic.asp?TOPIC_ID=44971

It explains the concepts of adding a <meta> tag to a master page that you can then set from a content page. The same principle applies to styles sheets. However, instead of adding a meta tag, add this to the head section:

<link runat="server" id="MyLink" href="~/Css/Styles.css"
        rel="stylesheet" type="text/css" />

Then in the code behind, you can control the MyLink tag's Href attribute like this:

MyLink.Href = "SomeNewStyleFile.css"

If you wrap the previous line in a property like StyleSheetUrl, similar to the meta tag, content pages can then programmatically set the style sheet.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Spirit by Dead Can Dance (Track 9 from the album: Dead Can Dance 1981-1998 (Limited Edition) [CD2]) What's This?
Reply With Quote
  #3 (permalink)  
Old June 8th, 2006, 05:13 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:
Sorry to be getting back with you so late. Got diverted to another project. You know how it is.

I have read and implemented the material you wrote and the link to topic 44971. I've had partial success in that I can *see* the master page from the content page. The unsuccessful part is that it does not work. To much to explain here.

Please visit http://www.cayuca.net . There you will see my current project. Attention to page ActivityAdd.aspx, which is the only content page developed so far.

You can view the code, but it appears to be much more complicated than it really is. So, you can also visit: http://www.cayuca.net/ActivitiesAddProblem.doc to see a more lucid writeup of what I have written. Somewhere in this MS Word doc I've made a mistake. Help please. Thanks.

VictorVictor

Reply With Quote
  #4 (permalink)  
Old June 8th, 2006, 05:32 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops. Made a minor boo-boo. The css should be applied to the label: lblLeaderName
and not the textbox txtLeaderName. Doesn't make any difference tho, still does not work.
VV


Reply With Quote
  #5 (permalink)  
Old June 9th, 2006, 02:57 AM
Imar's Avatar
Wrox Author
Points: 67,082, Level: 100
Points: 67,082, Level: 100 Points: 67,082, Level: 100 Points: 67,082, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,200
Thanks: 65
Thanked 1,404 Times in 1,384 Posts
Default

Hi VictorVictor,

Take a look at the *html* source of the final page in the browser.

Do you see something like:

<span id="__ctl00_ContentPlaceHolder1_lblLeaderName">Bla </span>

instead of

<span id="lblLeaderName">Bla</span>

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #6 (permalink)  
Old June 9th, 2006, 11:09 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:
I found the html you mention. Doesn't have the double leading underscores, but is the span tag for the lblLeaderName. There is only one place where this exists.

What I did was to add three more rules in the associated css to account for all possible id-s for this label: That is: #__ctl00_ContentPlaceHolder1_lbllblLeaderName {bla), same as previous but with only one leading underscore, and same a previous but with no leading underscore: #ctl00_ContentPlaceHolder1_lbllblLeaderName {bla}.

Still no joy.

I think I am missing your point somewhere. Should I change the rendered code from the remote server at run time in some event handler? I tried moving my instantiation of the masterpage.master and resetting the css page's url code from the pre-init handler to the pre-render handler. No joy there either.

VV


Reply With Quote
  #7 (permalink)  
Old June 9th, 2006, 12:09 PM
Imar's Avatar
Wrox Author
Points: 67,082, Level: 100
Points: 67,082, Level: 100 Points: 67,082, Level: 100 Points: 67,082, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,200
Thanks: 65
Thanked 1,404 Times in 1,384 Posts
Default

When you have a master page, you'll find that .NET changes the client side ID of your controls, to reflect the control hierarchy they're in. So, when you have a text box with an ID of txtUserName, it may come out completely different, with prefixes for the content placeholder etc.

Don't worry about the underscores. I couldn't remember the exact syntax when I typed that message, so I just made something up.

Two ways to fix it:

1. Look at the client code. If the ID is something like :

ctl00_ContentPlaceHolder1_lbllblLeaderName

then add the following to your stylesheet:

#ctl00_ContentPlaceHolder1_lbllblLeaderName
{

}

You said you habe that already, so something else must be going wrong. Make sure you applied the correct style sheet (maybe there's a path issue to the style sheet in your master page? Check the HTML source so see if the page is referencing the correct style sheet).

2. Use a class instead of an ID:

<asp:TextBox id="SomeId" CssClass="SomeClass" />

Then in your CSS add this:

.SomeClass
{
  // CSS definition goes here.
}

If all this doesn't work, please post your code. For me, that works a lot better than posting links to .doc files...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Page Method: Code-Behind for Content Page kwilliams ASP.NET 2.0 Professional 3 June 2nd, 2008 12:57 PM
Master Page and Content Page with Java script Harjinder ASP.NET 2.0 Professional 1 February 1st, 2008 09:31 AM
Master Page refresh only the content skyler ASP.NET 2.0 Professional 5 May 2nd, 2007 06:52 AM
how to add css to master pages? shaileshcdac Classic ASP Basics 0 April 5th, 2007 04:01 AM
Access Master page control from Content page. angshujit ASP.NET 2.0 Basics 3 January 11th, 2007 05:20 AM



All times are GMT -4. The time now is 04:18 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.