Porting old pages to use masterpages
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(Eve开发者_C百科ntArgs 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.
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.
精彩评论