Search code examples
asp.netvisual-studio-2010master-pagesdesigneronpreinit

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


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. alt text

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


Solution

  • 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">