Referencing a public property in current master page, from content page - asp.net

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;

Related

Master Pages over User Controls

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.

Finding master page control from within iFrame

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.

How to display content in iframe without Master Page

I have a iframe where it get and display of a page.
However if I display the page directly it may contain the header and footer from Master Page.
My question is can I display only the content of the page without the Master Page in iframe?
Please give suggestions and advises. Thank you.
Don't set the master page property of the page within the iframe.
You could probably pass a parameter into the page (through the querystring for example), which, when set, switches the masterpage to one that is effectively blank (since your page wont work without a masterpage).
So something like this in the Page_PreInit method
if (Request.QueryString["iframe"] == "true")
MasterPageFile = blankMasterPageFile;

master page menus

I want to create a master page for my already developed project.Since the project contains many forms it is quite difficult to include the master page in each form...Is there any possibilities to include the master page in any other simplest way...
Please give some suggestions..
Thanks in advance...
As far as I know, there is no easy way to do this.
You'll have to manually add the masterpage to the page directive
<%# Page MasterPageFile="~/Masterpage.master" ... %>
add the relevant content sections around your pages markup:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
and remove the <html>, <head>, <body> and <form> tags from every page.
Update
Here is an article (and source code) by Bob Powell describing a way to automatically convert html files to aspx files and add a master page. I'm sure you could adapt it to your needs.
This transition is not an easy one to make, as #geoff indicates. It is possible though if you have enough time and patience. The first step is to take all common elements (layout, menu, header, footer, whatever is common) and develop a master page structure. You'll likely need more than 1 depending on the differing layouts of forms in your application. Develop a user control for each of these common sections and make sure the master pages use these controls. Then systematically go through each page of your site and begin implementing the master pages.
As an assistance mechanism, you'll also probably want to have a page baseclass that is capable of communicating through the master page to the contained user controls. In our group we have a standard for setting a property on UserControls and MasterPages called ParentForm that is of type of our primary base page class, and this property is set during the Init of any page or control so that at any time, the developer has access (through Intellisense) to page itself. This is especially helpful since the parent of most controls is a container whose parent is a container whose parent ... you get the idea. For our controls it's just this.ParentForm.
It will be a long process, but MasterPages were really intended to be a "ground up" architectural decision rather than an "employ later" concept.

Can a 'content page' send simple 'properties' to a 'master page'?

The basic idea of a masterpage is simple -- you have a block of content that you want to 'inject' into a location in a master page.
Its very easy to figure this out without even reading the documentation about masterpages (which I admit to not having read!).
What i want to do is pass 'properties' to a master page from its child. For instance I may have a main content panel for which i want to set the padding in pixels in the child page. There may be other simple 'primitive types' that I want available to the master page to render its children. I want to avoid messing with style sheets as much as possible - or I'll end up with a tonne of similarly named items.
Is there a prefered way to do this?
In your master page create some type of hook (a property or method) that allows some piece of code to manipulate the master page in whatever way you want. Then on the individual pages do something like this:
YourMasterPageType masterPage = (YourMasterPageType)Page.Master;
masterPage.YourHook();
Whatever you do, do NOT do things the other way around (creating special cases within the master page that search the page for some magic value). You want to provide an interface to manipulate the master page, otherwise you'll end up with very messy code eventually.
Yes, but the other way round.
If you set the MasterPageType property of your content page you can access all public properties of the master page using the Master.PropertyName syntax.
So your child page can get and set property values of the master page.

Resources