使用asp.net中的代码更改母版页时出错

时间:2014-03-28 19:27:28

标签: asp.net

我有2个母版页 " Main.Master"和#34; 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主页必须在运行时更改这是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>

任何想法? ,由于

1 个答案:

答案 0 :(得分:0)

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