ASP.net c#Masterpage问题

时间:2010-09-13 11:52:33

标签: c# asp.net master-pages

我正在尝试使我的母版页使用我的内容页面,允许内容页面访问母版页控件。我收到错误:

  

分析器错误消息:'mastertype'   指令必须只有一个   attribute:TypeName或VirtualPath

这就是:

<%@ 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" %>

我的母版页是:

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

我对命名空间有点混淆,所以可能会让它们出错,但现在所有的页面都在同一个命名空间中。

更新

当我把它变成:

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

我一直收到错误:

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:        }

5 个答案:

答案 0 :(得分:3)

除非您想在当前页面(编写此代码)中保留母版页的引用,否则我建议您删除<%@ MasterType VirtualPath="MasterPages/Main.master" %> 线。

此行提供了访问页面母版页的方法(例如,当您要更改母版页上的标签或菜单需要添加更多项目等时)。 如果您的母版页的内容不需要从您的内容页面进行任何更改/更新,则无需使用MasterType标记。因为同时使用MasterPageFile="MasterPages/Main.masterMasterType,你混淆了编译器(即使主页是相同的)。

<强>更新
如果必须保留MasterType标记,则从页面指令中删除MasterPageFile="MasterPages/Main.master属性。

答案 1 :(得分:2)

如错误所示,@MasterType只需要一个参数。试试吧:

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

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms228274.aspx

答案 2 :(得分:1)

(1)注释掉MasterType指令并编译网站 (2)取消注释masterType指令并将其与类型名称或虚拟路径放在一起,两者都将抛出错误

评论和取消注释的原因: 从逻辑上讲,当网站无法构建时,它就不会有 AlphaPackSite.Main已创建,因此会抛出错误 但是一旦你发表评论,并且代码中没有其他错误,你希望你的垃圾箱中有类型!

因此有更多机会使用comment > compile > uncomment种类的东西
MasterType的参考: http://msdn.microsoft.com/en-us/library/ms228274.aspx

答案 3 :(得分:0)

尝试:

<%@ MasterType TypeName="AlphaPackSite.Main"  %>

答案 4 :(得分:-1)

我在寻找另一个答案时看到了这个问题,并认为如果其他人有这个问题,我会尽快给出答案。

问题是您不希望在主名称调用中使用虚拟路径。相反,您需要使用TypeName并为您的路径添加下划线。以下是基于您的路径的正确标记:

<%@ MasterType TypeName="AlphaPackSite_Main" %>
相关问题