【问题标题】:Testing to see if ContentPlaceHolder content has been overridden by a child page?测试 ContentPlaceHolder 内容是否已被子页面覆盖?
【发布时间】:2010-11-05 16:41:28
【问题描述】:
我目前正在将 .net 1.1 应用程序迁移到 .net 3.5。
.net 1.1 应用程序有许多我想迁移到母版页的页面 + 用户控件。
我的问题是尝试以编程方式进行测试,以查看母版页的 contentplaceholders 内容是否已被子页面覆盖。
- 有可能吗?
- 是否有人有我可以查看的示例或参考资料?
提前致谢。
【问题讨论】:
标签:
asp.net
.net-3.5
master-pages
contentplaceholder
【解决方案1】:
页面可以与母版页通信,但反之则不行,因为 contentplaceholder 中的内容不属于母版页。将页面“注册”到母版页的最快方法是声明一个从 .NET MasterPage 继承的类并在该类中公开通信功能。
公共抽象类 MyMaster : System.Web.UI.MasterPage
{
公共 MyMaster() { }
public abstract void TellMeSomethingAboutTheContent(SomeArgs args);
}
然后在使用主页面的页面中,您可以执行以下操作:
protected void Page_Load(object sender, EventArgs e)
{
MyMaster master = Page.Master as MyMaster;
if (master == null)
return;
master.TellMeSomethingAboutTheContent(args);
}
当然假设您有一个 SomeArgs 类,其中包含您希望母版页了解的数据。