Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Accessing Master Page elements form the Content Page

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

like image 988
kristof Avatar asked Jan 20 '09 16:01

kristof


People also ask

What is the relationship between content page and master page?

master page. In the content page, you create the content by adding Content controls and mapping them to ContentPlaceHolder controls on the master page. For example, the master page might have content placeholders called Main and Footer.

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.


2 Answers

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;
like image 75
George Avatar answered Nov 09 '22 14:11

George


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;
like image 42
mbillard Avatar answered Nov 09 '22 15:11

mbillard