Search code examples
c#asp.netmaster-pages

ASP.net c# Masterpage problem


I'm trying to make my masterpage work with my content page, allowing the content page to access the master page controls. I get the error:

Parser Error Message: The 'mastertype' directive must have exactly one attribute: TypeName or VirtualPath

This is on the lines:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct"
    MasterPageFile="MasterPages/Main.master"
    title="Hi there!"
%>
<%@ MasterType TypeName="Main" VirtualPath="MasterPages/Main.master" %>

My master page is:

namespace AlphaPackSite
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

I'm a bit confused with namespaces and such so may have got them wrong, but all pages are in the same namespace now I beleive.

Update

When I turn it to:

<%@ MasterType VirtualPath="MasterPages/Main.master" %>

I keep getting the error:

Compiler Error Message: CS0030: Cannot convert type 'AlphaPackSite.Main' to 'ASP.masterpages_main_master'

Source Error:

Line 147:        public new ASP.masterpages_main_master Master {
Line 148:            get {
Line 149:                return ((ASP.masterpages_main_master)(base.Master));
Line 150:            }
Line 151:        }

Solution

  • Unless you want to keep a reference of your master page in the current page (in which this code is written), I'd suggest you remove the <%@ MasterType VirtualPath="MasterPages/Main.master" %> line.

    This line provides a way to access your page's master page (such as when you've to change a label on the master page or the menu needs to add a few more items etc.). If the content of your master page does not require any changes/updates from your content page there's not need to use the MasterType tag. Because by using both MasterPageFile="MasterPages/Main.master and MasterType, you're confusing the compiler (even though the master page is same).

    Update
    If you have to keep the MasterType tag, then remove the MasterPageFile="MasterPages/Main.master attribute from the page directive.