ASP.NET - access to the elements of the main page on the content page

Is it possible to access the elements of the main page on the content page?

Suppose I have MasterPage1 and ContentPage1 that inherit from MasterPage1, and MasterPage1 has a button: Button1.

Is it possible to change the property of this button on the content page, for example, to make Button1 invisible, inactive, etc.? How can i do this?

I am using .net2.0

+6
master-pages
source share
4 answers

You must put the link to MasterPage in your markup on the page / user.

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

Then, in code, you just drop the page. Wizards on your MasterPage and get access to its properties.

 MyMasterPage myMasterPage = (MyMasterPage)Page.Master; 
+9
source share

Yes ... if you need to do this on an aspx page using MasterPage, this will be:

 Button myButton = (Button)Master.FindControl("myButton"); myButton.Visible = false; 
+10
source share

Yes, they can, and there are several approaches to this.

The approach that I use is to create public methods on the main page that will modify / access the data on the main page. For example, I usually like to change the link style for the current page / category on which I am included, so I have a method on my main page, for example:

  Public Sub SetNavigationPage(ByVal MenuName As String) DirectCast(Me.FindControl(MenuName), HyperLink).CssClass = "MenuCurrent" End Sub 

Then, on 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 class name of my homepage.

One interesting problem I discovered is that I had difficulty using the Visibility property of .NET controls when trying to show / hide them this way. This is due to the rendering of the wizard pages and content. To solve this problem, I set the basic CSS style for both visible and hidden, and set the CssClass property accordingly.

+3
source share

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

Be careful that the control you use to execute the above command should not be inside the update panel.

+3
source share

All Articles