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

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

Admin

Thanks for your question Ken I'm investigating the answer and I'll get back to you.

Hi John, any luck on finding an answer?
Thanks
Ken

Admin(edited)

Hi Ken

I'm so sorry I didn't get back to you. I wasn't able to get a solution and then it fell off my radar. I was just reviewing outstanding tickets and came across this. Were you able to get a solution?

Thanks

John

Admin

Hi Ken

The Theme and MasterPage are set during DisplayPageBase.OnInti and it isn't possible to override that - however it is possible to manipulate the theme and masterpage using URL parameters.

The EnsureTheme method in DisplayPageBase includes this:

if (string.IsNullOrEmpty(theme) && Request.QueryString["PreviewTheme"] != null)
theme = Request.QueryString["PreviewTheme"];

And the masterpage can be set to use the print or text-only masterpage (set up in the website definition) by passing URL parameter TemplateType=P or TemplateType=T

Thanks

John



Contact us
Copyright © Advanced Solutions International, All rights reserved.