移植旧页面以使用母版页
我有数百个旧版 Web 表单页面,通过 BasePage 覆盖添加页眉和页脚,渲染
protected override void Render(HtmlTextWriter writer)
{
RenderHeader(writer);
base.Render(writer);
RenderFooter(writer);
}
新页面使用 MasterPage 进行默认行为。
我想知道是否可以从 BasePage 添加 asp:content 控件而不更改每个 *.aspx?
我做了一个小测试,只要 aspx 中没有内容,它就可以工作,
public partial class OldPage : Page
{
private MainContentTemplate mainContentTemplate;
protected override void OnPreInit(EventArgs e)
{
Page.MasterPageFile = "~/Site.Master";
mainContentTemplate = new MainContentTemplate();
AddContentTemplate("Main", mainContentTemplate);
base.OnPreInit(e);
}
}
public class MainContentTemplate : ITemplate
{
#region ITemplate Members
void ITemplate.InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl("Test string"));
}
#endregion
}
但是一旦我在前面的代码中添加一些内容,我就会收到: 内容控件必须是内容页或引用母版页的嵌套母版页中的顶级控件。
我不确定,但我认为重写 ControlCollection 控件可能会有所帮助,但我还没有找到解决方案。
I have hundreds of legacy webform pages adding header and footer via a BasePage overriding Render
protected override void Render(HtmlTextWriter writer)
{
RenderHeader(writer);
base.Render(writer);
RenderFooter(writer);
}
New pages uses a MasterPage for the default behavior.
I would like to know if it's possible to add the asp:content control from the BasePage without changing every *.aspx?
I made a small test that's working as long there's no content in the aspx
public partial class OldPage : Page
{
private MainContentTemplate mainContentTemplate;
protected override void OnPreInit(EventArgs e)
{
Page.MasterPageFile = "~/Site.Master";
mainContentTemplate = new MainContentTemplate();
AddContentTemplate("Main", mainContentTemplate);
base.OnPreInit(e);
}
}
public class MainContentTemplate : ITemplate
{
#region ITemplate Members
void ITemplate.InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl("Test string"));
}
#endregion
}
But as soon as i add something to the code in front I will receive:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
I'm not sure but I think that overriding ControlCollection Controls might help, but I haven't find a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个容易解决的问题,但我最终在旧代码库中进行了大量搜索和替换。这花了几天时间,但我认为这是值得的,因为每个页面现在都在相同的代码库上运行。它总是提醒我,保持前端干燥与照顾后端同样重要。
This is no easy fix but I ended up with doing a massive search and replace in the old code base. It took a couple of days, but I think it was worth the effort since every page is now running against the same code base. It will always remind me that keeping the front end DRY is quite as important as taking care of the back end.