views:

96

answers:

3

I'm trying to get my content page to be able to access an ASP:Literal on a master page.

I have my content page as:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct" Title="Hi there!" %>
<%@ MasterType TypeName="Main" %>

Then my master page called Main.master has:

<asp:Literal runat="server" ID="lblBasket" />

But from the content page when I try and do:

Master.basket.Text = "test";

I get:

Error 46 The type or namespace name 'Main' could not be found (are you missing a using directive or an assembly reference?)

The error is on the designer page:

public new Main Master {
    get {
        return ((Main)(base.Master));
    }
}

My master page code behind is:

namespace AlphaPack.MasterPages
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.IsLoggedIn = Request.IsAuthenticated;
        }

        public bool IsLoggedIn
        {
            get { return this.ViewState["isLoggedIn"] as bool? ?? false; }
            set { this.ViewState["isLoggedIn"] = value; }
        }
    }
}
A: 
<%@ Page Language="C#" MasterPageFile="~MasterPages/Main.Master" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct" Title="Hi there!" %>
x2
Exactly same error still
Tom Gullen
A: 
<%@ Page MasterPageFile="~/MasterPages/Main.master" .. %>
<%@ MasterType VirtualPath="~/MasterPages/Main.master" .. %>

Okey, that's how it looks in my own app:

Master page (Site.master, in the root):

<%@ Master Language="C#" AutoEventWireup="True" CodeBehind="Site.master.cs" Inherits="Project.SiteMaster" %>

It's code-behind:

namespace Project
{
    public partial class SiteMaster : System.Web.UI.MasterPage { } 
}

Content page (Test.aspx, in the root):

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Test.aspx.cs" Inherits="Project.Test" MasterPageFile="~/Site.master" Title="Test" %>

it's code-behind:

namespace Project
{
    public partial class Test : System.Web.UI.Page { }
}

That's how auto-generated code looks like:

namespace Project {
    public partial class SiteMaster {            
        /// <summary>
        /// lblBasket control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Literal lblBasket;
    }
}

So create a property but don't share the control itself, only text:

public string BasketText
{
    get { return this.lblBasket.Text; } 
    set { this.lblBasket.Text = value; }
}
abatishchev
Exactly same error still
Tom Gullen
@Tome: See my updated post
abatishchev
+1  A: 

Is the designer within your AlphaPack.MasterPages namespace?

The MasterType isn't fully qualified, should it be? Don't you have to provide a path as well? (Not familiar with, sorry).

How does this respond if you use a MasterPageFile reference instead of a MasterType?

annakata
I was told to use the master type so that I can access variables on the master page.
Tom Gullen
I put them all in the same namespace and intellisense lets me see Master. now, but I get Error 21 'AlphaPackSite.Main' does not contain a definition for 'lblBasket' and no extension method 'lblBasket' accepting a first argument of type 'AlphaPackSite.Main' could be found (are you missing a using directive or an assembly reference?)
Tom Gullen
ah I can access functions on the master page now, but just can't access the label directly?
Tom Gullen
Thanks, nearly there I think! Even though the control exists on the master page it still can't find it: 'AlphaPackSite.Main' does not contain a definition for 'lblBasket' and no extension method 'lblBasket' accepting a first argument of type 'AlphaPackSite.Main' could be found (are you missing a using directive or an assembly reference?)
Tom Gullen
The references to controls are contained in the designer, this very much sounds like the designer needs to be regenerated still, but note also that ''AlphaPackSite' is not 'AlphaPack.MasterPages' as I would expect based on your posted code.
annakata