Set Property Value on Master Page from Content Page
Asked Answered
G

5

11

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.

Giraldo answered 2/7, 2009 at 0:6 Comment(0)
V
10

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*/ };
    }
}
Vole answered 2/7, 2009 at 0:12 Comment(0)
S
27

Add page directive to your child page:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Then add property to your master page:

public string Section { get; set; }

You can access this property like this:

Master.Section = "blog";
Sansculotte answered 24/7, 2010 at 17:1 Comment(1)
Voted this answer up over the others because it shows the use of the <%@ MasterType %> directive, which makes things work much better from content page - especially intellisense in Visual Studio.Idzik
M
15

Typecast Page.Master to your master page so that you are doing something like:

((MyMasterPageType)Page.Master).Roles = "blah blah";
Metaphosphate answered 2/7, 2009 at 0:10 Comment(2)
what is MyMasterPageType?Distichous
@Niloofar: When you create a master page, it creates a new class. In my example you'd replace MyMasterPageType with the name of your actual master page class.Metaphosphate
V
10

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*/ };
    }
}
Vole answered 2/7, 2009 at 0:12 Comment(0)
R
6

I'd go by creating a base class for all the content pages, something like:

public abstract class BasePage : Page
{
    protected abstract string[] RequiredRoles { get; }

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

        // display the required roles in a master page
        if (this.Master != null) {
            // value-assignment
        }

    }
}

And then I make every page inherits from BasePage, and each defining a RequiredRoles

public partial class _Default : BasePage
{
    protected override string[] RequiredRoles
    {
        get { return new[] { "Admin", "Moderator" }; }
    }
}

This has the advantage of cleanliness and DRY-ing the OnLoad handler code. And every page which inherits from BasePage are required to define a "RequiredRoles" or else it won't compile.

Ricker answered 2/7, 2009 at 1:4 Comment(1)
This one hasn't been upvoted enough. It's the correct approach on handling the requirement even if it's different from exact issue the OP was trying to solve.Metaphosphate
C
0

CType(Master.FindControl("lblName"), Label).Text = txtId.Text CType(Master.FindControl("pnlLoginned"), Panel).Visible = True

Clubhouse answered 27/12, 2017 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.