简体   繁体   中英

asp using master page variable in content page

I've read every post I can find on how to do this and tried them all. I have a Master page called DNAStaff (at root) containing:-

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="DNAStaff.master.cs" Inherits="DNAStaff" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
            <link href="~/Styles/DNA.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
     </asp:ContentPlaceHolder>

...............
    </div>
    </form>
</body>
</html>

The Masterpage code behind includes:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Data;

public partial class DNAStaff : System.Web.UI.MasterPage
{
    public string MyAccessLevel
    {
    get; set;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = "Data Source=NZ1;Initial Catalog=Intranet;Integrated Security=false;UID=IntranetAccess;PWD=*****";
        string sqlstring = @"select [MenuID], [Item], Target, SecLevel from [MENUS] ";
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlDataReader rdr = null;
        int MainGroup;
        try
        {
            if (Request.Cookies["userinfo"] != null)
            {
                MyAccessLevel = Server.HtmlEncode(Request.Cookies["userinfo"]["accessLevel"]);
            }
            else
            {
                MyAccessLevel = "1";
            }
    .........

The content page includes:

<%@ Page Title="" Language="C#" MasterPageFile="~/DNAStaff.master" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="main" %>
<%@ MasterType virtualpath="~/DNAStaff.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    Set Primary Project
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>

enter code here

and the content page code behind contains:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class main : System.Web.UI.Page
{
    string AccessLevel = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        AccessLevel = Master.MyAccessLevel;
        TextBox1.Text = AccessLevel;
    }
}

The line "AccessLevel = Master.MyAccessLevel" gives an error:-

Error CS1061 'MasterPage' does not contain a definition for 'MyAccessLevel' and no extension method 'MyAccessLevel' accepting a first argument of type 'MasterPage' could be found (are you missing a using directive or an assembly reference?) 8_Live_main.aspx D:\\Development\\dnanew.steelpencil.com\\Live\\main.aspx.cs 13 Active

I want to set the variable MyAccessLevel in the MasterPage at load and read it in any content page. I'm obviously missing something, can someone please help?

You can access the Master Page like this.

Site1 master = ((Site1)(Page.Master));
TextBox1.Text = master.AccessLevel;

Where Site1 is the class name of the Master Page ( public partial class Site1 : System.Web.UI.MasterPage )

However you should know that the Master Page_Load is initialized at a later stage in the Page Life Cycle than the Page. So when you access the value of AccessLevel , it will always be empty.

See https://msdn.microsoft.com/en-us/library/dct97kc3.aspx

As in the content page already you have assigned the MasterType like below.

<%@ MasterType virtualpath="~/DNAStaff.master" %>

So now your child/content page can able to use/access the master layout including the public variable/properties defined in the master page.

So now, as per your requirement, to access the public property( AccessLevel ) defined in your master page and assign to the child/content page textbox , you need to write the below code

Site myMaster = ((Site)(Page.Master));
TextBox1.Text = myMaster .AccessLevel;

Hope it solves your problem.

Here are the more references about Master page and accessing the contents of master page

https://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

https://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

This post should answer your question.
You must add

<%@ MasterType VirtualPath="~/your page path" %>

directive to your page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM