How to work with Master Page that is attached to the page via the page's basepage?

一世执手 提交于 2019-12-11 02:18:49

问题


In my ASP.Net web application, I have a base page that implements functionality that spans all pages of the web application and my web pages derive from this base page.

Since there is a single master page for the entire website, I don't want to attach the master page in each of the web pages. So I attached the Master page via the basepage's OnPreInit method as follows:

    protected override void OnPreInit(EventArgs e)
    {
        this.MasterPageFile = "~/Site.master";
        base.OnPreInit(e);
    }

However, when I switch to Designer view, I get the "Master Page Error"; The page has controls that require a Master Page reference, but noe is specified. Correct the problem in Code View.

When I run the application, the webpage shows up correctly.

What should be done so that the designer shows up correctly without having to go and set the master page explicitly in each of the web pages?

BTW, I am on Visual Studio 2010 and .Net 4.0


回答1:


You are in luck you can set the masterpage file in your web.config, and it will show in the designer without being explicitly set on the page. I just tested it out. Just look for the line below and put in your master page file.

<pages masterPageFile="~/MasterPage.master">


来源:https://stackoverflow.com/questions/4080393/how-to-work-with-master-page-that-is-attached-to-the-page-via-the-pages-basepag

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