I ended up creating two interfaces to solve this problem, as it wasn't a code behind file i couldn't just add a reference to the control/master page in the source.
I made my usercontrol inherit this interface
public interface IDeniedAccessControl
{
bool ContentVisible
{
get;
}
}
And my master page inherit this one
public interface IMasterPage
{
void AddControl(Control control);
ControlCollection MainContentControls
{
get;
}
}
I then made my usercontrol and master page implement there respective interface with the desired functionality.
Master page implementation:
public void AddControl(Control control)
{
ContentPlaceHolder1.Controls.Add(control);
}
public ControlCollection MainContentControls
{
get { return ContentPlaceHolder1.Controls; }
}
User control implementation:
bool IDeniedAccessControl.ContentVisible
{
get { return ContentASPxPanel.Visible; }
}
I could then use this on any code file. Below is how i edited my original code.
public class AdminBase : BasePage
{
IDeniedAccessControl deniedAccessControl;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//All pages that inherit from this will have a denied access control which will display when an admin of one instance tries to access admin pages of a different instance
//which they don't have access to. This enables us to still use the built in web.config authroisation API
deniedAccessControl = (IDeniedAccessControl)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");
((IMasterPage)Master).AddControl((Control)deniedAccessControl);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (deniedAccessControl.ContentVisible)
{
foreach (Control control in ((IMasterPage)Master).MainContentControls)
{
//only sets to false if the control is set to true as it will error if you try to set a non visiual controls "Visible" property. E.g an sqldatasource control
if (control.GetType() != deniedAccessControl.GetType() && control.Visible)
control.Visible = false;
}
}
}
}