以编程方式更改MasterPage [英] Change MasterPage programmatically

查看:71
本文介绍了以编程方式更改MasterPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我的ASP.net项目中,我在根文件夹中有两个文件夹:

1- Admin文件夹:它具有Admin MasterPage和一些页面.

2-成员文件夹:它具有成员MasterPage和一些页面,其中一个称为Comment.aspx


在某些情况下,当我通过以下方式转到Comments.aspx时:

Hi guys,

In my ASP.net project i have two folder inside root folder:

1- Admin folder: it has Admin MasterPage and some pages.

2- Member folder: it has Member MasterPage and some pages one is called Comments.aspx


in some case, when i go to Comments.aspx by:

Server.Transfer("~/Members/MemberComments.aspx");




我想通过以下方式将其MasterPage从Member MasterPage更改为Admin MasterPage:




i want to change its MasterPage from Member MasterPage to Admin MasterPage by:

protected void Page_PreInit(object sender, EventArgs e)
{
    this.MasterPageFile = "~/Admins/AdminMaster.master";
}





但它给我的错误是:执行子请求时出错.


我认为这是因为每个MasterPage都位于不同的文件夹中.

然后,请问有什么解决方法吗?





but it give me error that: Error executing child request.


I think that because every MasterPage in different folder.

Then, is there some solution please?

推荐答案

您需要使用内容页面的Page_PreInit方法.在这里可以动态设置母版页.

看看确切的讨论和步骤 [ ^ ]此处.
You need to use the Page_PreInit method of the content page. This is the place where Master page can be set dynamically.

Have a look at the exact dicsussion and steps[^] here.


制作两个母版页,名为Dynamic1.master和Dynamic2.master.

然后使用配置文件属性来访问母版页.
make two master pages named Dynamic1.master and Dynamic2.master.

and then use profile properties to get access the master pages.
<configuration>
<system.web>
<profile>
<properties>
<add

name="MasterPageFile"

defaultValue="Dynamic1.master" />
</properties>
</profile>
</system.web>
</configuration>




然后像这样创建dynamic1.master的内容页面.




then make a content page for dynamic1.master like this.

<asp:Content

ID="Content1"

ContentPlaceHolderID="ContentPlaceHolder1"

Runat="Server">
Select a Master Page:
<ul >
<li>
<a href="DynamicContent.aspx?master=Dynamic1">Dynamic Master 1</a>
</li>
<li>
<a href="DynamicContent.aspx?master=Dynamic2">Dynamic Master 2</a>
</li>
</ul>
</asp:Content>



母版页只能在preinit事件中调用.



masterpage can only be call in preinit event.

protected void Page_PreInit(object sender, EventArgs e)
{
if (Request["master"] != null)
{
switch (Request["master"])
{
case "Dynamic1":
Profile.MasterPageFile = "Dynamic1.master";
break;
case "Dynamic2":
Profile.MasterPageFile = "Dynamic2.master";
break;
}
}
MasterPageFile = Profile.MasterPageFile;
}


这篇关于以编程方式更改MasterPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆