在asp.net中获取母版页错误

时间:2013-05-14 15:52:40

标签: asp.net

我正在尝试在asp.net中添加一个母版页,但我收到的错误是该页面包含附加到母版页时无效的标记。这是我的母版页文件:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" 
Inherits="Pages_AdminMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: right">
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Log  Out</asp:LinkButton>

    </asp:ContentPlaceHolder>
</div>
</form>

以下是我应用该主页的网页表单:

<%@ Page Language="C#" MasterPageFile="~/Pages/MasterPage.master" 
AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Pages_Admin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    .style1
    {
        text-decoration: underline;
        color: #0000FF;
    }
    .style2
    {
        font-family: 15;
        font-size: 12pt;
        color: #0000FF;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style2" style="text-align: right">

</div>
</form>
<p class="style1">
    <a href="CreateCourse.aspx">Create a new course</a></p>
<p class="style1">
    <a href="ModifyCourse.aspx">Modify a course</a></p>
<p class="style1">
    <a href="CreateAccount.aspx">Create a new account</a></p>
<p class="style1">
    <a href="DeleteAccount.aspx">Delete an account</a></p>
<p class="style1">
    <a href="DisableAccount.aspx">Disable an Account</a></p>
<p class="style1">
    <a href="DropStudent.aspx">Drop a Student from a course</a></p>
<p class="style1">
    <a href="EnrollStudent.aspx">Enroll a student to a course</a></p>
<p class="style1">
    <a href="GetStudentInfo.aspx">Get Student info</a></p>
<p class="style1">
    <a href="GetInstructorInfo.aspx">Get Instructor info</a></p>
</body>
</html>

以下是浏览器中的错误:

Server Error in '/Bannerweb' Application.

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) +8968123
   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

这两个页​​面都位于名为Pages的文件夹中。谁能帮我这个?感谢。

3 个答案:

答案 0 :(得分:2)

使用母版页的网络表单,您需要将您的内容放在<asp:Content ContentPlaceholderID="ContentPlaceHolder1" runat="server"></asp:Content>内,该内容映射要在母版页中的特定页面上显示的内容。

MSDN提供了有关如何在主页上使用<asp:ContentPlaceHolder />以及在http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx使用<asp:Content />的更多信息。

答案 1 :(得分:1)

当您在visual studio中“添加新项目...”时,您应该选择“Web窗体”,然后在窗口的右下方,在您填写的“名称”框的右侧,选中复选框说“选择母版页”。

执行此操作将在单击“添加”后为您提供另一个对话框,以选择新Web表单应使用的母版页。

您的新网络表单将包含<Content>个标签 - 通常用于标题和内容;取决于您的母版页。网络表单不会包含所有常用的<doctype><head>标记等。

不是试图屠杀当前页面以获得正确的架构,而是开始一个新的架构,只需引入您需要的代码。

答案 2 :(得分:0)

尽管MSDN已经很好地解释了这种情况,但即使您已将代码放在<asp:content>代码中,也可能会出现同样的错误。

检查您是否在**<B> </B>**标记内的某处使用了<TD> </TD>标记,它会显示错误消息,将其转换为小<b> </b>标记,您将能够看到设计部分。

由于