Attaching master page dynamically to a conetnt page

| Tuesday, January 28, 2014

As the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then.
 protected void Page_PreInit(object sender, EventArgs e)  
       if (AdminPages)  
         this.Page.MasterPageFile = "~/Admin.master";  
         this.Page.MasterPageFile = "~/General.master";  
     catch (Exception ex)  

The MasterPageFile property can be set only in the PreInit event; attempting to set the MasterPageFile property after the PreInit event will throw an InvalidOperationException exception. If the MasterPageFile property is not valid, an exception of type HttpException is thrown later in the page life cycle, but no exception is thrown when the property is set in the PreInit event.


Post a Comment

Next Prev