我不能将 asp linkbutton 标记添加到 asp.net/c# 中的母版页吗

发布于 2024-10-25 15:00:41 字数 2072 浏览 2 评论 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

请帮助我。

谢谢你的期待

I'm trying to build a master page. What I'm trying to do is have a header in the master page with login and register link buttons (which would change to username and sign out) and there are content pages like home.aspx, shoppingcart.aspx, checkout.aspx which would inherit the master page with that header. So in that process i wrote the following code in master page (First.Master). And i tried to inherit the master page in home.aspx through this following code Inherits="SampleMasterProject.First" in the page tag

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

But I get following error

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

Please help me.

Thank you in anticipation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2024-11-01 15:00:41

听起来您没有以正确的方式使用母版页。内容页不会从母版页继承,而是使用“MasterPageFile”属性引用它。

First.Master

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

It sounds like you're not using Master Pages the right way. The content pages would not inherit from the master page, but instead reference it using the "MasterPageFile" property.

First.Master

<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
    {
    }
}
烟雨凡馨 2024-11-01 15:00:41

内容页不能从 MasterPage 继承。以下是有关母版页的一些文档: http://msdn.microsoft.com/en-us /library/wtxbf3hh.aspx

Content pages can't inherit from MasterPage. Here's some documentation on MasterPages: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文