Although master pages and content pages are powerful, their creation and application processes are not complicated. This section and the next section will create an example of how to create a master page and content page by using Visual Stuido 2005, for example, as shown in Figure 1, and related knowledge. The focus of this section is how to create a master page.
The master page contains the public part of the page, which is the page template. Therefore, before you create the example, you must decide which content is the public part of the page, and this requires starting from the analysis page structure. A screenshot of the page is shown in Figure 1. In the following section, the page is called Index.aspx and is assumed to be a page in a Web site. The analysis shows that the structure of the page is shown in Figure 5.
Figure 5 Page Structure Chart
The page index.aspx consists of 4 parts: header, footer, content 1, and content 2. Where the header and footer are common parts of the page in the Index.aspx site, many of the pages in the site contain the same header and footer. Content 1 and 2 are non-public parts of the page and are unique to index.aspx pages. With the knowledge of master pages and content pages, if you use master pages and content pages to create page index.aspx, you must create a master page masterpage.master and a content page index.aspx. Where the master page contains the contents of the header and footer, the content page contains content 1 and content 2.
Create a normal Web site using Visual Studio 2005, and then create a master page named Masterpage.master under the root directory of your site. Because this is the process of adding a new file, click Add New item from the Web Site Command menu. option, you can open the window as shown in Figure 6.
Figure 6 Adding a master page
Because this example creates a master page, you need to select the master page icon and set the file name to Masterpage.master. Note that there is also a check box item in the window that "places the code in a separate file." By default, this check box is selected. Represents Visual Studio 2005 will apply the code-behind model for the Masterpage.master file, which is to automatically create a MasterPage.master.cs file associated with the file, based on the creation of the Masterpage.master file. If you do not select the item, only one Masterpage.master file is created. It is recommended that the reader select the item.
After you create the Masterpage.master file, you can then start editing the file. As explained earlier, the master page contains only the common parts of the page, so the Masterpage.master contains the page headers and footer code. The specific source code looks like this: