Не могу ли я добавить тег asp linkbutton на главную страницу в asp.net/c#

Я пытаюсь создать главную страницу. То, что я пытаюсь сделать, это иметь заголовок на главной странице с кнопками входа и регистрации (которые изменятся на имя пользователя и выход), и есть страницы контента, такие как home.aspx, shoppingcart.aspx, checkout.aspx, которые будут наследовать главную страницу с этим заголовком. Итак, в этом процессе я написал следующий код на главной странице (First.Master). И я попытался унаследовать главную страницу в home.aspx с помощью следующего кода Inherits="SampleMasterProject.First" в теге страницы

<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

Пожалуйста помогите.

Спасибо в ожидании


person Sree    schedule 20.03.2011    source источник


Ответы (2)


Похоже, вы неправильно используете мастер-страницы. Страницы содержимого не наследуются от главной страницы, а вместо этого ссылаются на нее с помощью свойства «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>

Home.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>

Home.aspx.cs

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

    public partial class Home : Page
    {
    }
}
person Mun    schedule 20.03.2011

Страницы содержимого не могут наследовать от MasterPage. Вот некоторая документация по MasterPages: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

person Kon    schedule 20.03.2011