Can a 'content page' send simple 'properties' to a 'master page'? - asp.net

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.

Related

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 do I create a function common to all the pages in ASP.net?

I'm making a website that has a menu on all the pages that needs to be changed frequently. Right now I'm copying the code of the menu in all the pages, and so, if the menu needs to be changed, I have to open each page and change the menu there.
Is there any way by which I could define a function in a file somewhere that just writes the menu code using Response.Write() and then keep calling that function on all the pages that need the menu? That way whenever I'd need to make changes to the menu, I'll only make it in the function definition.
Have a look at a user control or master pages. Be more elaborate, so we can help you better.
It sounds like you should consider using a user control for the menu.
Master page is the perfect candidate.
Add the menu in master page.
You can also create a user control for this.
But if you want a function for doing a common task through out the site then create a class
and add the function is that class. Create an object of that class and call the function
anywhere in the site.
If you define a Menu in your Master Page it should refelect in all child pages.
All you need to do is create a Master Page and define your Menu in that.
After that you should create your child page using option >> WebForm using MasterPage
Apart from that for any other functions which are in Master page you can call them in your child page like this:
You can call method inside a MasterPage like this:
var master = Master as MyMasterPage;
if (master != null)
{
master.Method();
}

Referencing a public property in current master page, from content page

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;

ASP.NET Nested masterpages, how to set content in the top page from the aspx file?

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.

Best approach for styling navigation using unordered lists in ASP.NET

In ASP.NET (not MVC), what is the best approach to programmatically setting styles on an unordered list used for navigation so the appropriate menu item is styled as the active item if that page is being viewed?
This would most likely be used in conjunction with a MasterPage.
The answer to your question depends a lot on how you have your list implemented {User control or not, etc}. How I would do it, is implement the list to be generated by a user control.
I'd have the UserControl tag each element with something like:
<{...} class="GeneratedMenuItem"> {...}
And I'd have the appropriate styles in the Style Sheet; of course if it was in a user control then you might be able to use Themes (but that depends on which version of ASP.net you are using.
Good question, I have played around with various methods of doing this since the bad old days of asp, and am yet to find the perfect hammer.
Generally I have used the Request.Url.AbsoluteUri (or similar) as an argument to whatever the rendering function was, and most often set a css class of "current" or similar on the appropriate node, as well as rendering child nodes as needed.
I have most often used an xml/xsl combination, which can usually be worked against most cms platforms, although I have never been that happy with the overhead of firing up an xsl transform just to output a nav list, but if you know xsl, is a very nice tool for generating html, and you can always cache the output - for little static html sites which come up occasionally, I often use this approach in a build process to render static menu markup.
Have also used the aspnet sitemap functionality a few times, which is pretty good if you use the css friendly adapters with it - the default rendering makes very ugly markup.
I found this article earlier this weeek: http://blog.devarchive.net/2008/01/auto-generate-strong-typed-navigation.html which uses t4 templates to make a strongly typed navigation class, and I will definately be investigating that further.
The way I took this approach was to create ASP.NET Hyperlink Controls for each of my navigation items in my master page.
Within the master page, I then created a public method that would assign the appropriate "selected" CSS style to the control I specify:
Public Sub SetNavigationPage(ByVal MenuName As String)
DirectCast(Me.FindControl(MenuName), HyperLink).CssClass = "MenuCurrent"
End Sub
Then in my content pages, I simply had to reference the master page accordingly.
Dim myMaster As EAF = DirectCast(Me.Master, EAF)
myMaster.SetNavigationPage("hypSearchRequest")
This gave me the flexibility to add/remove navigation items on various pages and also be able to assign multiple pages to the administrator navigation option when necessary.
It is also interesting to note that referencing the hyperlink control and setting the Visibility attribute (for hiding administrative pages) didn't work. This is due to the order in which the master and the content pages load. Instead, I created another CSS class that simply set the visibility property and used the same approach as above.

Resources