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; }
}
}
}
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
?
<%@ Page Language="C#" MasterPageFile="~MasterPages/Main.Master" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct" Title="Hi there!" %>
<%@ 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; }
}
精彩评论