These docs are for v20.3.44. Click to read the latest docs for v20.3.186.

Discussions

Ask a Question
Back to All

Changing Masterpage and Theme via Code

I would like to be able to change the Masterpage and Theme via code. It needs to be done in the OnPreInit stage in the code. I have tried using the Global.asax and can change the Masterpage in the Pre_Init, but I cannot access OnPreInit - which is where Microsoft recommends these updates be done (see link at bottom).
Is it possible to access/override OnPreInit? For example:
void OnPreInit (EventArgs e)
{
Page page = this.Context.Handler as Page;
page.Theme = "tms2021";
page.MasterPageFile = "~/Templates/MasterPages/tms2021.master";
base.OnPreInit(e);
}
Microsoft ref: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.onpreinit?view=netframework-4.8
Thanks in advance.
Ken