c# - ASP.net c# 母版页问题

标签 c# asp.net master-pages

我正在尝试使母版页与内容页配合使用,从而允许内容页访问母版页控件。我收到错误:

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

最佳答案

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

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

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

关于c# - ASP.net c# 母版页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3700116/

相关文章:

c# - 如何检测正在运行的 ASP.NET 版本

c# - ASP.NET MVC5 中的密码恢复

c# - 如何使用 LINQ to SQL 还原关联记录

javascript - 使用jquery计算gridview中的行总计和总计

c# - 在 VS 2017 中针对完整的 .net 框架 asp.net 核心项目运行 XUnit 2.2.0 单元测试时出现 System.BadImageFormatException

asp.net - 如何在ASP.NET中通过局域网访问您的网站

c# - 访问在回发时以编程方式添加的控件

asp.net-mvc-3 - Asp.net MVC Site.master 和 Razor 开关

c# - ASP.NET MVC - 如何设计动态 H1/标题方案?

asp.net - 在 ASP.NET 中使用母版页时使用 JQuery 的正确方法是什么?