Frage

I am trying to convert an ASP.net web application into a website. One issue i am coming across is trying to access usercontrols and masterpages in my code files.

The code below shows a class i am using for a base class, it inherits from System.Web.UI.Page.

public class AdminBase : BasePage
{
    DeniedAccess 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 = (DeniedAccess)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");

       ((MasterPage1)Master).MainContent.Controls.Add(deniedAccessControl);

    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (deniedAccessControl.ContentVisible)
        {
            foreach (Control control in ((MasterPage1)Master).MainContent.Controls)
            {
                //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;
            }
        }
    }


}

The "DeniedAccess" control is a user control i am dynamically adding if the user fails some custom authorization.

This worked fine when this was a web application as the DeniedAccess control was compiled into the same dll so i could access it from the code. Now that it is a website it cannot find the namespace/class as it is not in the App_Code folder.

Is what I'm trying to do possible in a website project? I can't seem to find a way and the only alternative i see is having to write this code for each individual page rather than use it on a base page.

I also get the same problem when trying to cast the master page to my "MasterPage1" class for the same reason, it can't find it so i cannot access it's properties.

War es hilfreich?

Lösung

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;
            }
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top