google

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)  
   {  
     try  
     {  
       if (AdminPages)  
         this.Page.MasterPageFile = "~/Admin.master";  
       else  
         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.

0 comments:

Post a Comment

Next Prev
▲Top▲