How to Set Master Page dynamically?

耗尽温柔 提交于 2019-11-30 01:28:41

问题



I have requirement in which i want to set different Master Pages for the Same Page depending upon userid(i.e. for one user it must set one master page and for another user it must set another master ).Can we set different Master pages for any page dynamically?Please help...


回答1:


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

Explanation: You can attach a master page dynamically to a content page. 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.




回答2:


Please note this article on MSDN:

http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

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



回答3:


You can, by setting the MasterPageFile property of the Page. However, this will throw an InvalidOperationException if it is called after the PreInit event. Have a look at the ASP.NET Page Lifecycle

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.



来源:https://stackoverflow.com/questions/10930965/how-to-set-master-page-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!