Event to set the Master Page programatically at runtime in ASP.NET

By Santhosh N

This explain the page life cycle event that is best suited to set the Master Page for the webpage in ASP.NET programatically at runtime.

As the Master Page needs to be applied to the Page we desire to set and all the controls and hierarchy needs to be loaded appropriately, the setting of the Master page programatically at runtime requires to be set in proper event.

First of all to set the Master Page dynamically to the Page, we can do it by setting Page's MasterPageFile property and this should be set in the Page_PreInit event or any other event prior to this event. If this is set in any event after this event in the life cycle causes the following exception:

"The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event"

Related FAQs

This explains the equivalent of global.asa of classic ASP in Asp.Net
This explains about the co-existence of ASP and ASP.Net websites in the same server
This explains the equivalent of ObjectRecordSet.MoveNext ofclassic ASP in Asp.Net
Event to set the Master Page programatically at runtime in ASP.NET  (947 Views)