我无法在asp.net/c#中向asp页面添加asp linkbutton标签

时间:2011-03-20 17:29:56

标签: asp.net master

我正在尝试构建母版页。我要做的是在主页面中有一个标题,带有登录和注册链接按钮(可以更改为用户名和注销),还有像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

请帮帮我。

感谢您的期待

2 个答案:

答案 0 :(得分:3)

听起来你并没有以正确的方式使用Master Pages。内容页面不会从母版页继承,而是使用“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
    {
    }
}

答案 1 :(得分:0)

内容页面无法从MasterPage继承。以下是有关MasterPages的一些文档:http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx