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

前端 未结 4 743
情书的邮戳
情书的邮戳 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条回答
  •  -上瘾入骨i
    2020-12-10 06:16

    Just in case anyone stumbles across this and tears their hair out with a "Content controls have to be top-level controls in a content page or a nested master page that references a master page" error when trying Andy's code, get rid of the this.Master. So, the code becomes:

    protected void Page_PreInit(object sender, EventArgs e)
    {
        MasterPageFile = "/Site2.Master";
    }
    

    Edit As Zhaph points out below, the code I have ^^ there will only change the current page's master, not the master's master. This is the code Hainesy was talking about when he mentioned "we all know about changing a master page dynamically" (which I didn't, d'oh). If you happen to get to this page by googling "stackoverflow change master page" (which is what I did) then this is possibly the code you're looking for :-)

提交回复
热议问题