Search code examples
c#asp.netmaster-pagespage-lifecycle

When in the page lifecycle we can assign master page?


I know ViewState is available between InitComplete and Preload events in LoadViewSate method. Similarly, I want to know in which page lifecycle event can we assign a master page for a particular page?


Solution

  • Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage

    On Page PreInit event

    void Page_PreInit(Object sender, EventArgs e)
    {
        this.MasterPageFile = "~/MyMaster.master";
    }
    

    Read Working with ASP.NET Master Pages Programmatically