Search code examples
asp.netmaster-pagesroles

Set Property Value on Master Page from Content Page


I need to pass data to a variable in my master page each time a page is loaded.

I have a string[] of RequiredRoles that I set on each content page defining what roles are required to access that page.

On my master page, I have a method that takes this array, and checks to see if the current user is in one or more of those roles.

How would I go about managing this? I basically want each page to have a String[] RequiredRoles defined, and the master page will load this on each call and check to see if the users are in those roles.


Solution

  • Create a property in your master page and you access it from content page:

    Master page:

    public partial class BasePage : System.Web.UI.MasterPage
    {
        private string[] _RequiredRoles = null;
    
        public string[] RequiredRoles
        {
            get { return _RequiredRoles; }
            set { _RequiredRoles = value; }
        }
    }
    

    Content Page:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load()
        {
            Master.RequiredRoles = new string[] { /*set appropriate roles*/ };
        }
    }