コンテンツページからマスターページ上のプロパティ値を設定します。

StackOverflow https://stackoverflow.com/questions/1071920

  •  21-08-2019
  •  | 
  •  

質問

私は私のマスターページに変数にページがロードされるたびにデータを渡す必要があります。

私は[]私は役割がそのページにアクセスするために必要とされるものを定義する各コンテンツページで設定RequiredRolesの文字列を持っています。

私のマスターページに、私はこの配列を受け取るメソッドを持っており、現在のユーザーは、これらの役割の1以上であるかどうかをチェックします。

私はこれを管理についてどのように行きますか?私は基本的には、各ページには、[] RequiredRolesが定義されている文字列を持つようにしたい、とマスターページには、各呼び出しでこれをロードし、ユーザーがこれらの役割であるかどうかをチェックします。

役に立ちましたか?

解決

あなたのマスターページにプロパティを作成し、コンテンツページからアクセスます:

マスターページ:

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*/ };
    }
}

他のヒント

あなたの子ページにページディレクティブを追加します:

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

次に、あなたのマスターページにプロパティを追加します:

public string Section { get; set; }

あなたはこのように、このプロパティにアクセスすることができます:

Master.Section = "blog";

型キャストPage.Masterあなたのマスターページへのあなたのような何かをやっているようにます:

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

私は、すべてのコンテンツページの基本クラスを作成することによって、のようなものをいいよ

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ハンドラコードをDRY-INGの利点があります。そして、BasePageクラスから継承するすべてのページには「RequiredRoles」を定義する必要があるか、そうでなければコンパイルされません。

ctype関数(Master.FindControl( "lblName")、ラベル)の.text = txtId.Text   ctype関数(Master.FindControl( "pnlLoginned")、パネル).Visible = Trueの

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top