MasterPage问题 - ASP.NET Webform

时间:2016-06-27 21:10:09

标签: c# asp.net webforms

我已经在我的项目中添加了一个母版页,现在当我尝试将此母版页继承到一个页面时,我收到此错误:

'内容控件必须是内容页面中的顶级控件或引用母版页的嵌套母版页。'

master_page.Master:

@foreach($users as $user)
        <tr role="row" class="odd">
           @foreach($user->role as $r)
             <td>{{ $r->name }}</td>
           @endforeach`
        </tr>
@endforeach

的Index.aspx:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="master_page.master.cs"Inherits="KitchenCounter.master_pages.MasterPage"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title> 
        <asp:Content runat="server" ContentPlaceHolderID="MainContent">
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
        </asp:Content>
</head>
<body>
    <asp:Content runat="server" ContentPlaceHolderID="MainContent">
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
            <div class="menuBar">
                <ul>
                    <li class="hvr-bounce-to-bottom active"><a>Home</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../account/account.aspx">Account</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../recipes/recipes.aspx">Recipes</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../contact/contact.aspx">Contact</a></li>
                </ul>
            </div>
        </asp:ContentPlaceHolder>
    </asp:Content>
</body>
</html>

任何想法从哪开始?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

带有内容页面的母版页背后的想法是,您的母版页包含应显示在您网站中每个(或大多数)页面上的项目,其中占位符位于内容页面将填充特定于该页面的信息的位置。考虑到这一前提,有一些事情需要改变:

  1. 您将内容控件放在母版页中;他们需要进入内容页面,在您的情况下是Index.aspx。
  2. 此外,您的内容页面应仅包含其顶级控件标记 - 您使用ContentPlaceHolderId属性将内容页面上的标记内容与主页面上内容所属的位置相匹配。
  3. 最后,导航栏的标记需要在主页面的控件之外。控制就是 - 一个占位符;没有任何内容。

    所以试试这个:

  4. master_page.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="master_page.master.cs"Inherits="KitchenCounter.master_pages.MasterPage"%>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <div class="menuBar">
                <ul>
                    <li class="hvr-bounce-to-bottom active"><a>Home</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../account/account.aspx">Account</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../recipes/recipes.aspx">Recipes</a></li>
                    <li class="hvr-bounce-to-bottom"><a href="../contact/contact.aspx">Contact</a></li>
                </ul>
            </div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </body>
    </html>
    

    Index.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="KitchenCounter.pages.Index"  MasterPageFile="~/kitchencounter/master_pages/master_page.Master"%>
    
    <asp:Content runat="server" ContentPlaceHolderID="head">
        <title>Kitchen Counter | Home</title>
        <link rel="stylesheet" href="kitchencounter/css/main.css" />
    </asp:Content>
    <asp:Content runat="server" ContentPlaceHolderID="MainContent">
        The text that is specific for your Index page goes here.
    </asp:Content>