Search code examples
c#asp.netmaster-pages

How to change a MasterPageFile programmatically


I have a master page, and this master page has the following code

<%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="../sales/sales.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
   </asp:Content>

Now here is the issue i am having.. this is a copy of another master page within a site, but I have multiple sites that will be using it and I just want to make it common to all but I still want to access "..sales/sales.master" the problem is that this is within each site folder, for example

Web/
mySite1
   sales
     sales.master
mySite2
   sales
     sales.master

But now I need to have a master that will do the same for all

Web/
  All
    mynewmaster.master
  mySite1
     sales
        sales.master
  mySite2
     sales
        sales.master

so in mynewmaster.master I want to call MasterPageFile="../sales/sales/master" but I can't because 1) it would not find it and 2) it would not know which one to look at..

sales.master has all the css everything I need.. + other stuff I will add..


Solution

  • I believe you can only change the master from OnPreInit:

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);    
        Page.MasterPageFile = "MasterPage.master";
    }