0

マスターページを作成しようとしています。私がやろうとしているのは、マスター ページにログインと登録のリンク ボタン (ユーザー名とサインアウトに変更される) を含むヘッダーがあり、home.aspx、shoppingcart.aspx、checkout.aspx のようなコンテンツ ページがあることです。そのヘッダーを持つマスター ページを継承します。そのプロセスで、マスターページ(First.Master)に次のコードを書きました。そして、ページタグの次のコード Inherits="SampleMasterProject.First" を使用して、home.aspx のマスターページを継承しようとしました

<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="Header" runat="server">        
            <asp:LinkButton ID="UserNameLinkBtn" Text="UserName" runat="server"></asp:LinkButton>
            <asp:LinkButton ID="PwdLinkBtn" Text="Password" runat="server"></asp:LinkButton>
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

しかし、次のエラーが発生します

Content controls have to be top-level controls in a content page or a nested master page that references a master page.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Content controls have to be top-level controls in a content page or a nested master page that references a master page.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): Content controls have to be top-level controls in a content page or a nested master page that references a master page.]
   System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8836686
   System.Web.UI.Page.get_Master() +54
   System.Web.UI.Page.ApplyMasterPage() +15
   System.Web.UI.Page.PerformPreInit() +45
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328

私を助けてください。

楽しみにありがとう

4

2 に答える 2

3

マスター ページを正しく使用していないようです。コンテンツ ページはマスター ページから継承されませんが、代わりに "MasterPageFile" プロパティを使用して参照されます。

ファーストマスター

<html>
<head>
    <title>My Web App</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="LoginLinkBtn" Text="Login" runat="server" />
        <asp:LinkButton ID="RegisterLinkBtn" Text="Register" runat="server"/>

        <asp:ContentPlaceHolder ID="MainContent" runat="server">
            This content will be replaced on each page
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

ホーム.aspx

<%@ Page MasterPageFile="~/First.Master" Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MyApp.Home" %>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <h3>Content For Home</h3>
</asp:Content>

ホーム.aspx.cs

namespace MyApp
{
    // This still inherits from the Page class as usual

    public partial class Home : Page
    {
    }
}
于 2011-03-20T18:54:25.703 に答える
0

コンテンツ ページは MasterPage から継承できません。MasterPages に関するドキュメントは次のとおりです: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

于 2011-03-20T18:32:20.060 に答える