2010-09-13 54 views
2

我试图让我的母版页与我的内容页面一起工作,允许内容页面访问母版页控件。我得到的错误:ASP.net c#Masterpage问题

Parser Error Message: The 'mastertype' directive must have exactly one attribute: TypeName or 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:  } 

回答

3

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

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

更新
如果你要保持中的MasterType标签,然后从页面指令的MasterPageFile="MasterPages/Main.master属性。

+0

的'更新'标题下。这个答案是错误的。如果没有MasterPageFile属性,则不能使用MasterType指令,这会导致错误。 MasterType指令只允许您定义所使用的主控的类型,否则将始终默认为System.Web.UI.MasterPage,并且您无权访问继承的类成员和方法,而无需每次调用​​都进行类型转换。请参阅http://stackoverflow.com/questions/8946742/why-we-use-master-type – needfulthing 2017-04-03 12:37:13

0

尝试:

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

感谢您的回答,因为其他的答案,我已经更新的问题 – 2010-09-13 12:40:40

2

正如错误所述,@MasterType只需要一个参数。请试试:

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

请参阅http://msdn.microsoft.com/en-us/library/ms228274.aspx了解更多信息。

+0

谢谢你写好了同样的错误,我已经试过了已经和它抛出一个不同的错误:s – 2010-09-13 12:16:15

+0

它现在产生了哪个错误? – 2010-09-13 12:20:28

+0

我把它放在问题描述 – 2010-09-13 12:33:21

1

(1)注释掉的MasterType指令和编译网站 (2)取消注释中的MasterType指令,并与任一类型的名称或虚拟路径放置,都将抛出错误

原因注释和取消注释: 从逻辑上讲,当网站建立失败时,它不会有 AlphaPackSite.Main创建,因此会抛出错误 但一旦你注释掉了,并且代码中没有其他错误,你会希望得到你的箱子中的类型!

因此,有更多的机会与comment > compile > uncomment种事情的MasterType
参考工作: http://msdn.microsoft.com/en-us/library/ms228274.aspx

-1

我看到了这个问题,同时寻找其他的答案,想我会很快给出答案的情况下,其他人有这个问题。

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

<%@ MasterType TypeName="AlphaPackSite_Main" %>