add master page to an existing page
In Beta 2, can I add a master page for an existing page after its creation?
When I create a new ASP.NET page, if I do not check "Select master page", the new created page will not have a master page. And in the property pane, the MasterPageFile of the DOCUMENT is disabled. If I decide to add a master page for this web page later, what should I do?
The situation is that I have some web page upgraded from VS 2003. I need add master pages for these pages. Is that possible?
Thanks!
Zhu Ming
Hi Zhu Ming,
It is possible in code as well. The Page class exposes a property called the MasterPageFile.
This property needs to be set in the Page.PreInit event. Attempting to set the System.Web.UI.Page.MasterPageFile property after the Page.PreInit event will throw an InvalidOperationException.
Regards,
Vikram
Thanks, Vikram! But sorry it does not work. There will be an error:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
I know what I need to do now. In the Source wiew, remove the <!DOCTYPE>, <html>, <head>, <body>, <form>, and <div> elements from the page. Then add an <asp:Content> element that encapsulates all of the page’s remaining content.
I also tried to add the following line in web.config.
| | <pages masterPageFile="site.master" /> |
But for the existing page, it has no effect.
Anyway it seems that there is no solution that does not require changing the existing pages.
Regards! Zhu Ming