Posts Access Master Page Controls from Content Pages without using FindControl
Post
Cancel

Access Master Page Controls from Content Pages without using FindControl

Assume in the master page, we have a user control which we need to access and set its visiblity from content page. We create a public property in the master page called controlVisiblity

public bool LoginControlVisible

{

get

{

return LoginControl.Visible;

}

set

{

LoginControl.Visible = value;

}

}

To gain access to the master page file from the content page, you just need to add the below directive to the content page

<%@ MasterType VirtualPath=”~/siteMaster.master”%>

When ASP.NET realizes the existance of this directive, it generates a strongly typed property “Master”. this property will give you access to the MasterPage file. So directly in the content page code behind you would use the below instead of trying to use the FindControl method.

Master.LoginControlVisible = true;

or

Master.LoginControlVisible = false;

This post is licensed under CC BY 4.0 by the author.