ASP.NET - Accessing Master Page elements form the Content Page
Asked Answered
A

5

7

Can the elements of the Master Page be accessed from the Content Page?

Lets say I have MasterPage1 and ContentPage1 that inherits from the MasterPage1, and the MasterPage1 has a button: Button1.

Can I change the property of that button from the content page, for example to make Button1 invisible, inactive etc? How can I accomplish this?

I am using .net2.0

Aerolite answered 20/1, 2009 at 16:2 Comment(0)
S
9

You have to put a reference to the MasterPage in your page/user control markup.

<%@ Reference VirtualPath="..." %>

Then in the code-behind, you just cast the Page.MasterPage to your MasterPage and access its properties.

MyMasterPage myMasterPage = (MyMasterPage)Page.Master;
Sprint answered 20/1, 2009 at 16:6 Comment(0)
J
12

Yes...if you need to do this from the aspx page using the MasterPage it would be:

Button myButton = (Button)Master.FindControl("myButton");
myButton.Visible = false;
Jeremy answered 20/1, 2009 at 16:7 Comment(0)
S
9

You have to put a reference to the MasterPage in your page/user control markup.

<%@ Reference VirtualPath="..." %>

Then in the code-behind, you just cast the Page.MasterPage to your MasterPage and access its properties.

MyMasterPage myMasterPage = (MyMasterPage)Page.Master;
Sprint answered 20/1, 2009 at 16:6 Comment(0)
E
4

Master.FindControl("myButton").Visible = False

Be careful that the control that you use to run the above command, should not be inside an Update panel.

Extremely answered 28/7, 2009 at 11:1 Comment(0)
S
3

Yes they can, and there are a few approaches to this.

The approach I use is to create public methods within the master page that will do the modification/access to the data within the master page. For example, I typically like to modify the link style of the current page/category I am on, so I have a method in my master page like this:

   Public Sub SetNavigationPage(ByVal MenuName As String)

      DirectCast(Me.FindControl(MenuName), HyperLink).CssClass = "MenuCurrent"

   End Sub

Then in my content page, I simply access this method as such:

Dim myMaster As EAF = DirectCast(Me.Master, EAF)
myMaster.SetNavigationPage("hypViewEmployee")

...where EAF is the name of the class of my master page.

One interesting issue I've found is that I've had complications with using the Visibility property of .NET controls when trying to show/hide them in this manner. This is due to the rendering orer of master and content pages. To resolve this, I setup a basic CSS style for both visible and hidden and set the CssClass property accordingly.

Spindlelegs answered 20/1, 2009 at 16:7 Comment(1)
+1. Thanks for the tips with changing the CssClass - I find it very handy too. Somehow I forgot to leave the comment on the day the question was askedAerolite
S
0

I ran into an issue where I couldn't determine the control unless I looked for it within the ContentPlaceHolder. In this case a label (Label.ID = "lblMstrMessage"). My code follows (where ContentPlaceHolder.ID = "ContentTopPortion")

  Control cph = this.Master.FindControl("ContentTopPortion");
  Label mpLabel = (Label)cph.FindControl("lblMstrMessage");
Structuralism answered 23/12, 2020 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.