I have the following code in my ASPX side:
<%@ Page Language="C#" MasterPageFile="~/masterpages/standard.Master" .... %>
<%@ MasterType VirtualPath="~/masterpages/standard.Master" %>
I use the VirtualPath to access properties in my MasterPage.
So far, so good.
However, now I have to change masterpages programatically.
I know that to change a master page, I have to do on the page_init:
Page.MasterPageFile = "~/masterpages/myNewMasterPage.Master";
But I have no idea, of how to change the VirtualPath.
I assume you’re using MasterType because you need some property (which you would also need if you changed to another master), let’s say you are currently using
Master.MyButton, move this into a base class and use that type in your@MasterTypedeclaration:Now in your page, your MasterType declaration looks like:
Now when you change your virtual path, it doesn’t matter, you’re accessing properties in the base, same for both master pages.