Pass values from MasterPage UserControl to child ASPX Page - asp.net

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

Related

How to pass an ID from page to a user control's SelectParameter?

Struggling in the foothills of learning about web user controls (in VWD Express with VB), I'm looking for an efficient way to pass the value of an ID from the page to the control, to be applied in a datasource's SelectParameters. Any quick tips to get me started?
All child controls have access to their MasterPage via the Master property. If you make a public method on your MasterPage's code behind that returns the id, you can call that method from the child page.

how to use functionality from existing aspx page in usercontrol?

I am trying to use existing functionality which is embedded in a asp.net aspx page. So I have controls on this page and code behind for it. What is the best way to reuse this functionality in a new ascx control? Or do I just have to copy everyting into the control?
Can you not just take the controls you want to use from the aspx page and turn those into a control? Then you can use that control wherever you like.
To access the page parent just written this.Page in your control. But for access to these methods, you must apply an interface to the aspx page.

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.

Get all controls from MasterPage ChildPage

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

Render a ASP.NET control in the master page code-behind

I'm using a Substitution control in my master page, and I want to render a user control content (related to the login area of my website) in the Substitution .
Seems like I must have a reference for the requested page so that it could render the control. But I need to render the control in the master page itself, as it's shared across multiple pages in my website. What are the guidelines to achieve that?
Tks
So you want to render a user control from your MasterPage code-behind, and add it to a Substitution that's also in the master page? Why do you need a reference to the page that's using the master?
Assuming your using VB and that I understand your question, try this in your MasterPage code-behind:
Dim someControl As MyControl = CType((New Page()).LoadControl("~/Path/To/MyControl.ascx"), MyControl)
mySubstitution.Controls.Add(someControl)

Resources