Hello I have been stuck with this situation for quite some time? Looking for some help here?
I have defined Nested Master Page (1 Default for WebApp and other Custom Master Page referencing the Default One)
The following is the code snipet from content page(say content.aspx) consuming my Custom Master Page
in aspx source code I have included:
%# MasterType VirtualPath="~/NestedMasterPage1.master" %>
and in code behind file ie. custom.aspx.cs (This is where the problem is):
ContentPlaceHolder masterContentPlaceHolder = (ContentPlaceHolder)Page.Master.Master.FindControl("MainContent"); //works well
ContentPlaceHolder nestedContentPlaceHolderHeading = (ContentPlaceHolder)masterContentPlaceHolder.FindControl("NestedMasterHeading"); //works well
Label NewsHeadLines = (Label)nestedContentPlaceHolderHeading.FindControl("lblSubSectionHeader"); //returns null?? The Control ID is all checked and is the same in the Nested Master Page.
**NewsHeadLines.Text = "Testing";** //System.NullReferenceException:
Object reference not set to an instance of an object.
Is it happening because I have nested my Control(here the label) in HTML Tables,rows and columns? Please advise? And I have double checked the control names or IDs.
I believe you might be missing one or two nested containers. Try this; instead of you trying to figure out the nested containers try having a common programming logic to get them. Have a look at Rick Strahl's blog post
Related
I'm in the code behind of a user control. I need to access one of the page's properties (EnableSessionState).
Originally, this would be defined something like this in aspx:
<%# Page Language="C#" EnableSessionState="ReadOnly" %>
However, I want to dynamically change this value in the code behind.
I looked at this answer:
PagesSection pages = WebConfigurationManager.OpenWebConfiguration("").GetSection("system.web/pages") as PagesSection;
pages.EnableSessionState = PagesEnableSessionState.ReadOnly;
Unfortunately, I'm just trying to read the header. Also considered this answer:
PagesSection pagesSection = new PagesSection();
pagesSection.EnableSessionState = PagesEnableSessionState.ReadOnly;
But I'm not trying to read default values, I'm trying to set the actual page's values.
Unfortunately, there is no property like this.Page.PagesSection, so is there another way of going about this?
Since the goal was to modify session state behaviour, according to this blog, it is possible to access using the following line:
Context.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);
I hope someone can help me.
I have a header page that has a logo, menu, a search box, etc. For certain pages, I want to be able to hide some parts of that header.
I've created three master pages and three headers, but the differences in the headers are very small, so I'd rather not maintain three copies of the header.
I want to set a property in the master page, that I can reference in the header and hide content appropriately. The problem of course is that the header is used on pages that use several different master pages, and the only way I can find to access properties on a master page is to use
<%# MasterType VirtualPath="~/masters/SourcePage.master" %>
But that links to a specific master page, so it doesn't really help me. Is there a way to reference the currently used master page instead, and access its public properties?
Thanks.
Sorry I seem to have been thinking about it backwards. The header is in all the master page, so I can set a property on it from the master page. Blame the lack of sleep, and the lack of caffeine in this place! :P
You should be able to do something like
var result = ((MasterPageClassName)Page.Master).MasterPageClassProperty;
I have some content from a CMS that I need to move to raw asp.net pages. Since the templates are nested I guess I can use nested masterpages to acomplish it, but I'm finding that I can't set values on the top masterpage from the deep child page.
Here is a sample. I have several nested masterpages with contentplaceholders:
top master (with contentPlaceHolder1)
nested master, dependent on top master (with contentPlaceHolder2)
aspx page, dependent on nested master, defines content for contentPlaceHolder1 and 2
The problem is that asp.net doesn't allow me to have the value of contentPlaceHolder1 defined in the content page, it should be defined in the nested master. But the point is that the client page knows that value, not the template masters (for instance, the page knows about the graphic it has to display on the the top, but the placeholder for the graphic is the top master).
How can I set values in the aspx page to be rendered in the top master?
Normally you would have to do the following:
setup a public property on your master page
add the #MasterPage declaration to the top of any content page that you want to access the property in
access the property like Master.YourPageProperty = "value";
In the case of nested masterpages you must also setup pass-through public properties using the same method above but doing it in your nested master page (ie define the nested master pages master page and set up dummy public properties that just set the value passed in to the nested master to the top master).
Try looking at this article to reinforce the ideas I have touched upon:
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
I normally make all of my .aspx pages inherit from a base page, and on this page I set up properties for any data that I want to share. Then, on your top master page you can cast the current page to type basepage and then have access to all the data.
I usually expose the base page as a property on my master page also, that way I can do a soft cast and do null checking if I have any case where a page that doesn't inherit from my base page is using the master page.
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.
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)