【问题标题】:error when change master page with code in asp.net使用 asp.net 中的代码更改母版页时出错
【发布时间】:2014-03-31 01:40:06
【问题描述】:

我有 2 个母版页 “Main.Master”和“MainAr.Master” 我有页面“Default.aspx”从基类继承

public class BaseClass:Page
{
    protected override void InitializeCulture()
    {
        if (Session["Lang"] != null)
        {
            if (Session["lang"].ToString() == "ar")
            {
                this.UICulture ="ar";
                this.Culture = "ar-EG";

                this.Theme = "ar";
            }
            else if(Session["Lang"].ToString()=="en")
            {

                this.UICulture = "en";
                this.Culture = "en";
                this.Theme = "en";
            }

        }
        else
        {
            this.UICulture = "en";
            this.Culture = "en";
           // this.Theme = "en";
        }


    }
    protected override void OnPreInit(EventArgs e)
    {
        if (Session["Lang"] != null)
        {
            if (Session["lang"].ToString() == "ar")
            {
                this.Page.Master.MasterPageFile = "~/MainAr.Master";
            }
        }
    }
}

onPreinit void master page of default page must be changed in runtime 这是inhertnce的代码

 public partial class Default : Utilities.BaseClass

但在更改母版页代码时出现此错误

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

2 母版页代码是

<%@ Master Language="C#" AutoEventWireup="true" 
 CodeBehind="Main.master.cs"  Inherits="MotahedaWeb.Main" %>

 <!DOCTYPE html>

 <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">
 <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>
</form>
</body>

而default.aspx页面代码是

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master"
 AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MotahedaWeb.Default" %>
 <%@ MasterType VirtualPath="~/Main.Master" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 </asp:Content>

 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 </asp:Content>

有什么想法吗? ,谢谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    这似乎是正确的。您只需要确保 ContentPlaceholderIds 在您正在交换的页面之间保持一致(这就是导致该错误消息的原因)

    【讨论】:

    • 当然内容占位符在页面之间是一致的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多