Ошибка реализации ASP.NET Masterpage - PullRequest
0 голосов
/ 25 января 2012

Я использую VS2005 C # ASP.NET.

У меня есть страница .aspx со сценарием для управления членством пользователя.

Однако, когда я попытался внедрить Masterpage на мою .aspx страницу, я получил сообщение об ошибке

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

Я правильно сослался на свою главную страницу, и я не знаю, где я ошибся.

Нужна помощь в указании, где я ошибся на моей странице .aspx.

Спасибо


Ниже приведен мой код для моих .aspx и masterpage:

.aspx:

<%@ Page Language="C#" MasterPageFile="~/MainPage.master"%>

<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" >
<script Runat="Server">

  string[] rolesArray;
  MembershipUserCollection users;
  string[] usersInRole;

  public void Page_Load()
  {
    Msg.Text = "";

    if (!IsPostBack)
    {
      // Bind roles to ListBox.

      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      RolesListBox.DataBind();

      // Bind users to ListBox.

      users = Membership.GetAllUsers();
      UsersListBox.DataSource = users;
      UsersListBox.DataBind();
    }

    if (RolesListBox.SelectedItem != null)
    {
      // Show users in role. Bind user list to GridView.

      usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
      UsersInRoleGrid.DataSource = usersInRole;
      UsersInRoleGrid.DataBind();
    }
  }


  public void AddUsers_OnClick(object sender, EventArgs args)
  {
    // Verify that a role is selected.

    if (RolesListBox.SelectedItem == null)
    {
      Msg.Text = "Please select a role.";
      return;
    }


    // Verify that at least one user is selected.

    if (UsersListBox.SelectedItem == null)
    {
      Msg.Text = "Please select one or more users.";
      return;
    }


    // Create list of users to be added to the selected role.

    string[] newusers = new string[UsersListBox.GetSelectedIndices().Length];

    for (int i = 0; i < newusers.Length; i++)
    {
      newusers[i] = UsersListBox.Items[UsersListBox.GetSelectedIndices()[i]].Value;
    }


    // Add the users to the selected role.

    try
    {
      Roles.AddUsersToRole(newusers, RolesListBox.SelectedItem.Value);

      // Re-bind users in role to GridView.

      usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
      UsersInRoleGrid.DataSource = usersInRole;
      UsersInRoleGrid.DataBind();

      Msg.Text = "User(s) added to role: " + RolesListBox.SelectedItem.Text;
      return;
    }
    catch (Exception e)
    {
      Msg.Text = e.Message;
    }
  }


  public void UsersInRoleGrid_RemoveFromRole(object sender, GridViewCommandEventArgs args)
  {
    // Get the selected user name to remove.

    int index = Convert.ToInt32(args.CommandArgument);

    string username = ((DataBoundLiteralControl)UsersInRoleGrid.Rows[index].Cells[0].Controls[0]).Text;


    // Remove the user from the selected role.

    try
    {
      Roles.RemoveUserFromRole(username, RolesListBox.SelectedItem.Value);
    }
    catch (Exception e)
    {
      Msg.Text = "An exception of type " + e.GetType().ToString() +
                 " was encountered removing the user from the role.";
    }


    // Re-bind users in role to GridView.

    usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
    UsersInRoleGrid.DataSource = usersInRole;
    UsersInRoleGrid.DataBind();

    Msg.Text = "User(s) removed from role: " + RolesListBox.SelectedItem.Text;
    return;
  }



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>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 />
      <br />
    <table cellpadding="3" border="0">
      <tr>
        <td valign="top" style="width: 117px; height: 145px;">
          <font face="helvetica" size="3" color="#455c75"><strong>Roles:</strong></font></td>
        <td valign="top" style="height: 145px; width: 265px;">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" AutoPostBack="true" /></td>
        <td valign="top" style="height: 145px">
          <font face="helvetica" size="3" color="#455c75"><strong>Users:</strong></font></td>
        <td valign="top" style="height: 145px">
          <asp:ListBox ID="UsersListBox" DataTextField="Username" Rows="8" SelectionMode="Multiple"
            runat="server" />
            <br />
          <asp:Button Text="Add User(s) to Role" ID="AddUsersButton" runat="server" OnClick="AddUsers_OnClick" /></td>
        <td valign="top" style="height: 145px">
          </td>
      </tr>
      <tr>
        <td valign="top" style="width: 117px">
          <font face="helvetica" size="3" color="#455c75"><strong>Users in Role:</strong></font></td>
        <td valign="top" style="width: 265px">
          <asp:GridView runat="server" CellPadding="4" ID="UsersInRoleGrid" AutoGenerateColumns="False"
            GridLines="Horizontal" OnRowCommand="UsersInRoleGrid_RemoveFromRole" ForeColor="#333333">
            <HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
            <Columns>
              <asp:TemplateField HeaderText="User Name" >
                <ItemTemplate>
                  <%# Container.DataItem.ToString() %>
                </ItemTemplate>
              </asp:TemplateField>
              <asp:ButtonField Text="Remove From Role" />
            </Columns>
              <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
              <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
              <EditRowStyle BackColor="#999999" />
              <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
              <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
              <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
          </asp:GridView>
        </td>
      </tr>
    </table>
  </form>
</body>
</html>
</asp:Content>

Masterpage:

<%@ 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>RM</title>
    <link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="wrapper">

        <form id="form1" runat="server">

            <div id="header" style="height: 2.7em">
                <span class="title">Role Management</span>
                <span class="breadcrumb">
    <asp:SiteMapPath ID="SiteMapPath1" runat="server">
    </asp:SiteMapPath>
</span>
            </div>

            <div id="content">
                <asp:contentplaceholder id="MainContent"
                 runat="server">
                  <!-- Page-specific content will go here... -->
                </asp:contentplaceholder>
                <br />
                &nbsp;</div>
            <div id="navigation" style="top: 4.19em; left: 0.51em;">
            <asp:Panel ID="Panel1" runat="server" Height="50px" Width="260px">
                <font size="2">You are logged in as </font>
                <asp:LoginName ID="LoginName1"  runat="server" Font-Bold="False" Font-Underline="False" ForeColor="RoyalBlue" Font-Italic="False" />
                <br />
                <asp:LinkButton ID="Logout" runat="server" Font-Bold="True" Font-Underline="True"
                    OnClick="Logout_Click" Font-Size="Smaller" ToolTip="Logout">Logout</asp:LinkButton></asp:Panel>
            <ul>
                    <li>
                        <asp:HyperLink runat="server" ID="lnkHome"
         NavigateUrl="/SoD/Common/Default.aspx">Home</asp:HyperLink>

        <asp:Repeater runat="server" ID="menu" DataSourceID="SiteMapDataSource1" EnableViewState="False">
    <ItemTemplate>
        <li>
            <asp:HyperLink ID="HyperLink1" runat="server"
             NavigateUrl='<%# Eval("Url") %>'>
             <%# Eval("Title") %></asp:HyperLink>

            <asp:Repeater ID="Repeater1" runat="server"
                DataSource='<%# ((SiteMapNode) Container.DataItem).ChildNodes %>'>
                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>

                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="HyperLink2" runat="server"
                         NavigateUrl='<%# Eval("Url") %>'>
                         <%# Eval("Title") %></asp:HyperLink>
                    </li>
                </ItemTemplate>

                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </li>
    </ItemTemplate>
</asp:Repeater>
                    </li>

    </ul>
</div>
    <asp:SiteMapDataSource ID="SiteMapDataSource1"
      runat="server" ShowStartingNode="false" />
      </form>
</div>
</body>
</html>

1 Ответ

3 голосов
/ 25 января 2012

<asp:Content> пропускает runat = "сервер"

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server" >

Еще одна вещь: удалите следующие теги из файла .aspx <head>, <form>, <body> и <html>потому что эти теги унаследованы от главной страницы

...