正在将母版页添加到aspx网页

本文关键字:aspx 网页 添加 母版页 | 更新日期: 2023-09-27 18:20:47

我目前正在参考此网站进行AD角色管理。

C#代码运行良好,但当我将代码与主页一起粘贴到网页中时,页面给了我一个错误,上面写着:

Content controls have to be top-level controls in a content page or a nested master page that references a master page.

我可以知道在这种情况下我应该如何设置主页吗?

.没有母版页的ASPX页:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  string[] rolesArray;
  MembershipUserCollection users;
  string[] usersInRole;
  public void Page_Load()
  {
  ....
  }

  public void AddUsers_OnClick(object sender, EventArgs args)
  {
    ....
  }
  public void UsersInRoleGrid_RemoveFromRole(object sender, GridViewCommandEventArgs args)
  {
  ....
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Role Membership</title>
</head>
<body>
  <form runat="server" id="PageForm">
    <font face="helvetica" size="6" color="#455c75"><strong>Role Membership</strong></font><font face="helvetica" size="5" color="#455c75"><strong> Management</strong></font>
    <br /><asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table cellpadding="3" border="0">
      ...
    </table>
  </form>

主页:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainPage.master.cs" Inherits="MainPage" %>
<!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 id="Head1" runat="server">
    <title>SOD</title>
    <link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>
</html>

编辑:

我尝试将MasterPageFile="~/MainPage.master"插入到第一个标记中,该标记以<%@ Page Language="C#" MasterPageFile="~/MainPage.master"%>开始我的.aspx页面,结果出现了上面提到的错误。

正在将母版页添加到aspx网页

主页包含HTML代码和内容区域。使用母版页的页面必须具有<asp:Content>标签。在Content标记之外不能有任何类型的代码。

您需要将代码放入<asp:Content ID="content1" ContentPlaceHolderID="Content_pageBody" Runat="Server"></content>标记中。这个标签应该映射到您的主页面,其中ContentPlaceHolderID=主页面中的占位符名称