Get all controls from MasterPage ChildPage - asp.net

Is it possible to get all controls from a ChildPage from the MasterPage the child is nested in?
I need all the controls within my form tag, but when i use a MasterPage i cant see the controls from the ChildPage, only the ones in the MasterPage.
Anyone with a good solution on this one? :)

Suppose you have a Label control as [Label1] on the child page.
So to access that child page control from the master page, i would use
Label lbl = (Label)this.ContentPlaceHolder1.FindControl("Label1");
lbl.Text = "I got you";
So to get all the controls simply do this:
ContentPlaceHolder1.Controls

Here's a great article on some more advanced master page topics. I believe your situation is covered.
http://odetocode.com/articles/450.aspx

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.

Is it possible to create a master page which selects another master page?

I have a master page with name MasterPage.master.
Now I want to create another master page (MasterPage2.master) that its MasterPageFile is MasterPage.master,
But when I create MasterPage2 and then create a webform from MasterPage2, there isn't any ContentPlaceHolder in this webform.
Please explain for me...
What you're looking for is nested master pages.
Google it and you'll have a ton of tutorials. Top 2 results from google:
http://msdn.microsoft.com/en-us/library/x2b3ktt7(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/bb547109(v=vs.100).aspx
Most likely either:
MasterPage2 is using the ContentPlaceHolder from MasterPage but not adding another ContentPlaceHolder for your content page to use
the ContentPlaceHolder on MasterPage2 has the same IDs as the one on MasterPage
Make sure MasterPage2 has a ContentPlaceHolder with a unique identifier.

Pass values from MasterPage UserControl to child ASPX Page

Senario: Masterpage with a UserControl and a child ASPX page
In the past when using this senario I've used an Interface as a way to pass a value from the UserControl (embedded in a
master page) to the masterpage code behind then consume that value in the child aspx page.
My question is now that asp.net 4 have arrived is this still a good way to achieve this or is there another or perhaps better way to do it?
I've read somewhere that perhaps "delegates" is perhaps another route to take.
Any help much appreciated
Lk
You could have your Masterpage inheret from a BaseMasterPage class that inherits from System.Web.UI.MasterPage. Then you can have a property on the BaseMasterPage. The user controls and child aspx pages can access the property by ((BaseMasterPage)this.Page).PropertyName

ASP.NET MVC 1.0 Nested Masterpages

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.

show an aspx page in content tag

I have a master page(MyMasterPage.master) with two content place holders. I wanna show another aspx page(MyHeader.aspx) in the 1st content place holder of a content page(MyContentPage.aspx) that uses my master page.
You should be using user controls for reusable components of a pages. Create a Header.ascx file instead of a .aspx. You can then drag that into your ContentPlaceHolder from the solution explorer when in design mode.
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
I would use UserControls. MyHeader.ascx.
I echo the prior commenter's suggestion to use a usercontrol if possible.
If you really want to keep your existing aspx page; you might want to take a look at using an IFrame to accomplish this.

Resources