Can I change a nested master page's master dynamically?

前端 未结 4 747
情书的邮戳
情书的邮戳 2020-12-10 05:42

Okay, so we all know about changing a master page dynamically in a page\'s OnPreInit event.

But what about a nested master page? Can I change a master\'s master?

4条回答
  •  时光取名叫无心
    2020-12-10 06:20

    To add on to the answer of Zhaph - Ben Duguid, (+1 by the way):

    Here is example code that sets the master page of the nested master page. All pages inherit from this BasePage, so this code only exists in one place.

    public class BasePage : System.Web.UI.Page
    {
        private void Page_PreInit(object sender, System.EventArgs e)
        {
            if (Request.Browser.IsMobileDevice)
            {
                if (Page.MasterPageFile == "~/master/nested.master")) 
                {
                    Page.Master.MasterPageFile = "~/master/mobile.master";
                } 
                else 
                {
                    MasterPageFile = "~/master/mobile.master";
                }
            }
        }
    }
    

提交回复
热议问题