I have a problem with my master pages. They have such inheritance order:
MainMaster1.Master can be nested by Nested1.Master, Nested2.Master, etc.
At the same time MainMaster can be duplicated and have working copies like MainMaster2, MainMaster3, etc.
Advise please how can I dynamically change the MasterPageFile of my Nested1, Nested2, etc. pages so that they can easily switch between MainMasters if needed?
I tried to treat the problem in Page_PreInit of the nested masters but couldn't get it entering this event handler. I also tried to change the masters in "protected void OnPreInit" of nested ones but result was the same.
Thanks,
Roman.
See the following article for several possible techniques you can use:
Dynamically Switching between Master Pages in ASP.NET MVC http://www.codeofrob.com/archive/2009/11/01/dynamically-switching-between-master-pages-in-asp.net-mvc.aspx
I think that's still what he meant, although it doesn't directly answer the question.
Page has a property called MasterPageFile which is used in the above article
Page also has a property called MasterPage
On MasterPage, there is a property called MasterPageFile (As in the above article)
MasterPage also has a property called MasterPage
While the example on that page covers with changing the master page on the page directly, you can do very much similar to the master page itself by recursing up through the master pages to find and change the one you want.
I hope that helps.
Related
I'm working on a .NET WebForm app which has a master page. The .NET can not recognize many elements in the aspx file. For example it say Label is not a valid asp element and then says most likely reason is a malformed web.config file.
I double check my web.config and all looks good. The other webpages do not have same issue. The interesting thing is I do not have this issue inside the MasterPage.
I also tried recreate the page from scratch but still having same issue.
I'm using VS2019. Framework 4.6
You have to provide some sample code and markup. Also, if you have a master page, then that is a huge deal also. As a general rule, asp.net controls you drop on the page are able to be used from code behind. However, the code for the standard page(s) that we use as a "child" of a master pages means that code behind for the master page can easy use controls in master page. And code behind for the page being displayed in that master page ALSO can freely use its own controls. But controls between the master page and the working child page is VERY different matter.
And of course controls dropped into a repeater, or say listview (or even gridview) means that the one label or text box control is automatic repeated over and over. As such, you have to pull/get/use the one row out of that data bound repeating control, and then from that one repeating row grab the control in question.
So, saying I can't start my car, or I can't use or get a control?
We need more information as to the context of what control, where it is (in the master or child), and is the control perhaps nested inside of a data bound repeater, listview, gridview etc.
so, edit your question - add some details as to the markup, where it is (master or the child page), and we can help.
So, as a general rule, code behind in master page is free to use controls in the master page.
And in the web page you created, once again code behind is free to use controls in that page.
It can be more difficult to say have code in master page, and have it reference controls in the child page that is being displayed. But, then again, it is VERY rare that code in master page would need to reference or play with controls in the child page, since a master page will (usually) just be your main navigation bar - and it will be the same for many if not all pages you display - hence you master page really can't know what controls will exist in the current child page being displayed.
An interview question someone asked me but I was left stumped.
If you can implement user controls inside a normal aspx page, what is the use of a master page then ?
A master page defines a layout template. You put things like styling and scripts in the master page, then call them in any page derived from that master.
It's a way of being able to split presentation out from functionality, and things you need everywhere can be put in the masterpage.
I am trying to create reversible themes in ASP.NET. I can successfully change themes using a dropdown list, but I am running into problems changing SkinID's and generic HTML controls (which are all div's except for the body tag) programmatically. I moved all my attribute- and skin-changing code to the PreInit method of my Base Page. Now I get a NullReferenceException when I run the page. I thought this code was supposed to use the existing controls it is supposedly pointing to. What am I missing or doing wrong?
Here is my code:
The PreInit event is probably too early in the page lifecycle to look for controls with FindControl. You're also doing your declarations outside of the event. That may have something to do with your null reference as well. I'd see if you can change it to look later in the lifecycle. This may be helpful if you haven't already seen it: http://msdn.microsoft.com/en-us/library/ms178472.aspx
Scenario:
I have one main master page say MasterPage1. In that master page I have a splitter. In that splitter there is an iframe. Within that iframe we load another master page say "MasterPage2". In MasterPage2 we load a page on which different User Controls are rendered.
Problem:
Now I want to find a control on MasterPage1 from my User Control loaded on the page in MasterPage2.
Please help....
Problem To your Scenario:
masterpages and content pages are rendered as a single object, thus the page class is able to reference every element found in both the objects(master and content page). When you are rendering an iframe the iframe content is requested by client hence no reference exists. so it is not possible to reference each other on server.
Solution to the problem
From above you must have realized all the problem is the reference , so you will have to hack inti it. the simplest way I can think is to use querystring.
call the iframe page with querystring containing a identifier to the masterpage like mpage=mpage1,mpage=mpage2 etc.
Now in masterpage2 request the querystring to find which masterpage is applied and proceed. This way you will have little relaxaction because masterpage1 content cannot be changed but masterpage2 can be.
Now you will need to work more to what you need. Proceed only if this is the only way to solve the real problem(I think the problem is not masterpage but the solution to the problem that is making you to do these weired things).
Well for that you will have to use javascript and handlers which will render and return the rendered usercontrol. But i seriously say not to use this setup in production and find other alternative by changing your code to use usercontrol instead of iframe.
Currently I have a site that is set up using a masterpage and a nested master page. The master page setups up the header and footer info. The nested masterpage is used once logged into the site.
The issue I have is that I want to programmatically load a different masterpage to define different header and foot info.
I don't think I can use the OnPreInit() in each content class to set a different masterpage. I don't think I can do this because each Content page uses the Nested Masterpage.
What I would like to do is programmatically set which masterpage is called in the NestedMaster.
Any ideas?
I saw this blog posted on another MasterPage question. Before trying this route I wanted to see if anyone else has experienced this.
Thanks
EDIT:
In the CS page:
public class AdminBasePage : BasePage
{
protected override void SetMasterPageFile()
{
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
}
}
Look here:
Nested Master Pages
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
(Thanks for the answer Ken and Brad, however I read most of that long tutorial before spotting the simple one line of code had been added to your question, so am making it more obvious by adding it as an answer here and will edit your post. Cheers)