Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2006, 11:45 AM
Friend of Wrox
 
Join Date: Apr 2005
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

 
Old June 1st, 2006, 04:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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?
 
Old June 8th, 2006, 05:13 PM
Friend of Wrox
 
Join Date: Apr 2005
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

 
Old June 8th, 2006, 05:32 PM
Friend of Wrox
 
Join Date: Apr 2005
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


 
Old June 9th, 2006, 02:57 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
 
Old June 9th, 2006, 11:09 AM
Friend of Wrox
 
Join Date: Apr 2005
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


 
Old June 9th, 2006, 12:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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





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 10: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 06:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.