views:

2466

answers:

6

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

+2  A: 

See this:

Otávio Décio
Short, but accurate ;)
Zhaph - Ben Duguid
+2  A: 

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;
GoodEnough
A: 

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;
George
+1  A: 

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.

Dillie-O
+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 asked
kristof
A: 

CVan we find the same using javascript? if yes please let me know how. its urgent.

A: 

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.

then how can we disable control inside a update panel
ush
then how can we disable the controls in a update panel
ush