从内容页设置母版页上的属性值

发布于 2024-07-25 12:46:20 字数 238 浏览 2 评论 0原文

每次加载页面时,我都需要将数据传递给母版页中的变量。

我在每个内容页面上设置了一个RequiredRoles 字符串[],定义了访问该页面所需的角色。

在我的母版页上,我有一种方法可以获取该数组,并检查当前用户是否属于这些角色中的一个或多个。

我将如何处理这个问题? 我基本上希望每个页面都定义一个 String[]RequiredRoles,母版页将在每次调用时加载它并检查用户是否处于这些角色。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栀梦 2024-08-01 12:46:29

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

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

一袭水袖舞倾城 2024-08-01 12:46:28

在母版页中创建一个属性,然后从内容页访问它:

母版页:

public partial class BasePage : System.Web.UI.MasterPage
{
    private string[] _RequiredRoles = null;

    public string[] RequiredRoles
    {
        get { return _RequiredRoles; }
        set { _RequiredRoles = value; }
    }
}

内容页:

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

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*/ };
    }
}
一身仙ぐ女味 2024-08-01 12:46:28

我会为所有内容页面创建一个基类,例如:

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
        }

    }
}

然后我让每个页面都继承自 BasePage,并且每个页面都定义一个RequiredRoles

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

这具有整洁和干燥 OnLoad 处理程序代码的优点。 每个继承自 BasePage 的页面都需要定义一个“RequiredRoles”,否则无法编译。

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.

錯遇了你 2024-08-01 12:46:27

将 Page.Master 类型转换到您的母版页,以便您执行以下操作:

((MyMasterPageType)Page.Master).Roles = "blah blah";

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

((MyMasterPageType)Page.Master).Roles = "blah blah";
终难愈 2024-08-01 12:46:25

将页面指令添加到您的子页面:

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

然后将属性添加到您的母版页:

public string Section { get; set; }

您可以像这样访问此属性:

Master.Section = "blog";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文