9

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

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

在我的母版页上,我有一个采用此数组的方法,并检查当前用户是否处于其中一个或多个角色中。

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

4

5 回答 5

25

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

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

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

public string Section { get; set; }

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

Master.Section = "blog";
于 2010-07-24T17:01:22.877 回答
15

Typecast Page.Master 到您的母版页,以便您执行以下操作:

((MyMasterPageType)Page.Master).Roles = "blah blah";
于 2009-07-02T00:10:17.987 回答
10

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

母版页:

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*/ };
    }
}
于 2009-07-02T00:12:30.057 回答
6

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

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”,否则它将无法编译。

于 2009-07-02T01:04:15.070 回答
0

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

于 2017-12-27T09:12:53.510 回答