ProblemYou want to use the same master page for all pages in a section of your application, but you want the ability to change which master page is used without having to modify all of the affected pages, something you cannot do with the more traditional master/content page approaches described in the previous recipes in this chapter. SolutionImplicitly assign a master page to a content page as follows:
Example 1-5 shows the web.config file used to set the master page implicitly, and Examples 1-6 and 1-7 show the .master and .aspx files for our example. DiscussionIn some applications, it is desirable to use the same master page for a large number of content pages and, at the same time, to be able to change to another master page without having to edit all of the pages in the application. ASP.NET 2.0 provides the ability to assign the master page implicitly to content pages by using the new masterPageFile attribute of the <pages> element in the web.config file. Setting the masterPageFile attribute to the name of a .master file assigns the master page to all content pages in the folder where the web.config file is located. This assigns the master page to all content pages in all subfolders unless another web.config file in a subfolder overrides the setting. ASP.NET provides a lot of flexibility when using this approach. Any content page can still explicitly set the MasterPageFile attribute in the @ Page directive, as described in Recipe 1.1, which will override the setting in the web.config file. This is convenient when you have a small number of pages that need to be handled differently; however, this can become confusing when the assignment of a master page is changed in the web.config file and the developer is expecting it to affect all pages.
In our example, we have created a new folder and placed in it the web.config file shown in Example 1-5 along with the .master and .aspx files shown in Example 1-6 and Example 1-7. The primary difference between this example and the example shown in Recipe 1.1 is the removal of the MasterPageFile attribute from the @ Page directive of the content page and the presence of the web.config file.
See AlsoRecipes 1.1 and 1.2 Example 1-5. web.config file for implicitly assigning a master page to a content page
Example 1-6. Implicitly assigning a master page to a content page (.master)
Example 1-7. Implicitly assigning a master page to a content page (.aspx)
|