In the "htmlMaster", I've several contentPlaceHolders for title, javaScript and CSS includes and so forth. That way I can add titles directly from my specific templates and handle includes on sub template level, enabling me to cut away unnessesary script and CSS when I dont need it.
My products are rendered through a Macro based on a Usercontrol. When ever details of a product is displayed, I would like to control the title for instance. I just can't figure out how to do this from my Usercontrol. I've looked into Masterpages, and my logic approach would be to do something like this:
Works just fine in my example (htmlMaster/design/products). The only problem is, that when I place my title ContentPlaceHolder (which I use from all other templates than "products" to set title), it doesn't work anymore.
I also tried:
Page.Master.Master.FindControl("myTitle")
Page.Master.Master.Master.FindControl("myTitle")
Both returning null. "myTitle" is the Id of the ContentPlaceHolder tag in my "htmlMaster" template:
Perhaps I should mention, that I need to set several values including meta tags and such from my "products" Usercontrol - so the solution must not be limited to title.
I sence that my approach using ContentPlaceHolders is wrong - regarding adding title, javascript, css, meta etc. from sub templates. Can you explain how I should approach the issue instead?
If you have a runat="server" on the <head> html tag on your master page (<head runat="server">) you should be able to set the title from any page with:
this.Title = "The page title";
and from any usercontrol with:
this.Page.Title = "The page title";
You dont need to go up through the master pages to do that.
You are right enggaard - but what is the cleanest way, of implementing the possibility of using "Page.Title" from a Usercontrol and possibility of setting the title from templates or xslt as well?
As you can see there is a masterpage -> design.master -> products.master and a sample page wich inherits the products.master. Specific script you can put in the master, without coding. Don't put too many different scripts and css in pages. A css file is not that large and will be cached by the browser. Using small page-specific js/css files will slow the site.
Thanks for the example. You are right, small files are best served always, so the browser can cache the files. My approach is more based on not including jQuery and a ton of extensions, if they are not needed.
Thats pretty much my current setup, so maybe I'm missing something here - or not explaining my problem clearly enough :-)
With that setup, my "product.aspx" page is a node created in Umbraco using my "products" template. In the "products" template, a Macro based on a Usercontrol renders the products. In the current setup, how do I set values of the different ContentPlaceHolders in my master.master?
So,you put a usercontrol(as a macro) in the product node page. From the usercontrol you can access the controls from parents as mentioned in this thread before. If you would like to alter values from usercontrols, use actual controls and not contentplaceholders because they only serve as a placeholder in master page scenarios.
You can't "set values" in a contentplaceholder, because they just don't have a 'value'. It's an indication for nested masterpages to use that space for content.
If you would like to create values in the master, you can use a Placeholder control to add controls to: Placeholder.Controls.Add( etc... ).
I follow you so far, and I'm fully aware of what a ContentPlaceHolder is. But in my mind, I should be able to create a "asp:Content" object from codebehind (should be the same as from design, no?), and add this object to the masterpages title ContentPlaceHolder, right? I cannot find out how - and I'm unsure wether this is Umbraco specific problem or general ASP.NET problem.
I've also added a test PlaceHolder to my title in the master.master, but I cannot refer this PlaceHolder by either "Page.Master.Master.FindControl" or "Page.Master.Master.Master.FindControl". So how would you think I can refer this PlaceHolder?
Access to contentPlaceHolders from usercontrol?
I've got myself a new challenge :-)
My template structure (simplified) is as follows:
htmlMaster
design
normal
products
etc
In the "htmlMaster", I've several contentPlaceHolders for title, javaScript and CSS includes and so forth. That way I can add titles directly from my specific templates and handle includes on sub template level, enabling me to cut away unnessesary script and CSS when I dont need it.
My products are rendered through a Macro based on a Usercontrol. When ever details of a product is displayed, I would like to control the title for instance. I just can't figure out how to do this from my Usercontrol. I've looked into Masterpages, and my logic approach would be to do something like this:
ContentPlaceHolder ph = (ContentPlaceHolder)Page.Master.FindControl("title");
This always yields null :/
Can anyone point me in the right direction?
If you're trying to access nested master pages, I think you need the master of the masterpage. So I guess you'll need to use
If you put runat="server" on the title tag in your master,you don't need any placeholder. you can access the title directly:
runat="server" is not needed as fas I can see. You can access the master page title directly as I mentioned above.
Snippet
Thanks for your suggestions so far!
The code:
Works just fine in my example (htmlMaster/design/products). The only problem is, that when I place my title ContentPlaceHolder (which I use from all other templates than "products" to set title), it doesn't work anymore.
I also tried:
Both returning null. "myTitle" is the Id of the ContentPlaceHolder tag in my "htmlMaster" template:
Perhaps I should mention, that I need to set several values including meta tags and such from my "products" Usercontrol - so the solution must not be limited to title.
Martin,
FindControl is not recursive maybe try http://geekswithblogs.net/AzamSharp/archive/2008/04/20/121438.aspx that is a recursive findcontrol function.
Regards
Ismail
You should use a Literal control for the title. Contentplaceholder is used for (nested) masterpages.
Snippet
Ok Folkert,
I sence that my approach using ContentPlaceHolders is wrong - regarding adding title, javascript, css, meta etc. from sub templates. Can you explain how I should approach the issue instead?
If you have a runat="server" on the <head> html tag on your master page (<head runat="server">) you should be able to set the title from any page with:
this.Title = "The page title";
and from any usercontrol with:
this.Page.Title = "The page title";
You dont need to go up through the master pages to do that.
You are right enggaard - but what is the cleanest way, of implementing the possibility of using "Page.Title" from a Usercontrol and possibility of setting the title from templates or xslt as well?
Guys,
If you have runat at server in the head tag any macros and umbraco:item calls will render out literally its a know issue not sure if its fixed in latest version see http://our.umbraco.org/forum/umbraco-pro/contour/9213-macro-and-item-issue-when-head-runat-server
Regards
Ismail
Okay, here we go, for a base setup:
master.master:
Snippet
Design.master:
Snippet
products.master:
Snippet
product.aspx:
Snippet
As you can see there is a masterpage -> design.master -> products.master and a sample page wich inherits the products.master.
Specific script you can put in the master, without coding.
Don't put too many different scripts and css in pages. A css file is not that large and will be cached by the browser. Using small page-specific js/css files will slow the site.
Thanks for the example. You are right, small files are best served always, so the browser can cache the files. My approach is more based on not including jQuery and a ton of extensions, if they are not needed.
Thats pretty much my current setup, so maybe I'm missing something here - or not explaining my problem clearly enough :-)
With that setup, my "product.aspx" page is a node created in Umbraco using my "products" template. In the "products" template, a Macro based on a Usercontrol renders the products. In the current setup, how do I set values of the different ContentPlaceHolders in my master.master?
So,you put a usercontrol(as a macro) in the product node page. From the usercontrol you can access the controls from parents as mentioned in this thread before.
If you would like to alter values from usercontrols, use actual controls and not contentplaceholders because they only serve as a placeholder in master page scenarios.
You can't "set values" in a contentplaceholder, because they just don't have a 'value'. It's an indication for nested masterpages to use that space for content.
If you would like to create values in the master, you can use a Placeholder control to add controls to: Placeholder.Controls.Add( etc... ).
Thanks for your patience :-)
I follow you so far, and I'm fully aware of what a ContentPlaceHolder is. But in my mind, I should be able to create a "asp:Content" object from codebehind (should be the same as from design, no?), and add this object to the masterpages title ContentPlaceHolder, right? I cannot find out how - and I'm unsure wether this is Umbraco specific problem or general ASP.NET problem.
I've also added a test PlaceHolder to my title in the master.master, but I cannot refer this PlaceHolder by either "Page.Master.Master.FindControl" or "Page.Master.Master.Master.FindControl". So how would you think I can refer this PlaceHolder?
In master.master:
In page control:Snippet
Snippet I don't think you can't set the asp:content via Code-behind. Maybe in page-init event?
is working on a reply...