我有数百个遗留的网页表单页面通过BasePage覆盖渲染
添加页眉和页脚 protected override void Render(HtmlTextWriter writer)
{
RenderHeader(writer);
base.Render(writer);
RenderFooter(writer);
}
新网页使用MasterPage作为默认行为。
我想知道是否可以在不更改每个* .aspx的情况下从BasePage添加asp:content控件?
我做了一个很小的测试,只要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控件可能会有所帮助,但我找不到解决方案。
答案 0 :(得分:0)
这不是一件容易的事情,但我最终在旧的代码库中进行了大量搜索和替换。花了几天时间,但我认为这是值得的,因为每个页面现在都在针对相同的代码库运行。它总会提醒我,保持前端DRY与照顾后端一样重要。