ASP.NET MasterPageFile导致错误

时间:2010-08-12 12:23:15

标签: asp.net master-pages

我正在尝试在我的网站中使用母版页。我创建了一个页面然后创建了主页。然后我添加了标签MasterPageFile =“〜/ master”。我猜我需要对我的主页做一些事情,因为我现在开始收到错误了:

The page contains markup that is not valid when attached to a master page

我的主页如下:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/my.master" %>

<!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">
<body>
     <form id="form1" runat="server">
     <div>

     </div>
     </form>
</body>
</html>

我尝试删除标签,认为这可能是问题所在。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:5)

您需要将body更改为content部分。母版页与contentplaceholders一起使用:您的内容页面需要为这些占位符提供内容。 See the msdn page on this topic.

从上面的链接引用,您的母版页可能包含以下内容:

<td><asp:contentplaceholder id="Main" runat="server" /></td>

通过提供以下内容页面将填写

<% @ Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    Main content.
</asp:Content>

请注意,我在主页的顶部包含了主声明。

答案 1 :(得分:1)

继承母版页的页面应该以{{1​​}}为根。这意味着没有html标签没有doctype等。