【问题标题】:ASP.net c# Masterpage problemASP.net c# Masterpage问题
【发布时间】:2010-09-13 11:52:32
【问题描述】:

我正在尝试使我的母版页与我的内容页一起使用,从而允许内容页访问母版页控件。我得到了错误:

解析器错误消息:'mastertype' 指令必须恰好有一个 属性: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:        }

【问题讨论】:

    标签: c# asp.net master-pages


    【解决方案1】:

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

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

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

    【讨论】:

    • 这个答案是错误的。您不能使用没有 MasterPageFile 属性的 MasterType 指令,这将导致错误。 MasterType 指令只允许您定义使用的主服务器的类型,否则它将始终默认为 System.Web.UI.MasterPage 并且您无法访问继承的类成员和方法,除非每次调用都进行类型转换。见stackoverflow.com/questions/8946742/why-we-use-master-type
    【解决方案2】:

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

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

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

    【讨论】:

    • 谢谢,我已经试过了,但它会抛出一个不同的错误:s
    • 我把它放在问题描述中的“更新”标题下
    【解决方案3】:

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

    评论和取消评论的原因: 从逻辑上讲,当网站构建失败时,它不会有 AlphaPackSite.Main 创建,因此会抛出错误 但是一旦您将其注释掉,并且代码中没有其他错误,您将有望在您的 bin 中获得该类型!

    所以有更多机会使用comment &gt; compile &gt; uncomment 之类的东西
    MasterType 参考: http://msdn.microsoft.com/en-us/library/ms228274.aspx

    【讨论】:

      【解决方案4】:

      试试:

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

      【讨论】:

      • 感谢您的回答,与其他答案相同的错误,我已将其写入问题的更新中
      【解决方案5】:

      我在寻找另一个答案时看到了这个问题,并想我会尽快提供答案,以防其他人遇到这个问题。

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

      <%@ MasterType TypeName="AlphaPackSite_Main" %>
      

      【讨论】:

        猜你喜欢
        • 2010-10-05
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 2015-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多