Share via


How to: Create Content Pages for an ASP.NET Master Page

A content page is an ASP.NET Web page that is associated with a master page. The master page establishes a layout and includes one or more ContentPlaceHolder controls for replaceable text and controls. The content page includes only the text and controls that are merged at run time with the master page's ContentPlaceHolder controls. For more information, see ASP.NET Master Pages Overview.

Visual Web Developer provides a number of ways to create a content page, including a shortcut if you are editing a master page. You can also create a content page by starting with a blank page and adding the appropriate directive.

After creating a content page, you can create custom content that corresponds to each ContentPlaceHolder on the master page. Alternatively, you can explicitly choose to allow the master page's default content to be displayed.

To add a content page in Visual Web Developer

  1. In Solution Explorer, right-click the name of the Web site and then click Add New Item.

  2. In the Visual Studio installed templates list, click Web Form.

  3. Select the Select master page check box, and then click Add.

    The Select a Master Page dialog box appears.

  4. In the Contents of Folder box, click the master page that you want to associate with the page you are creating, and then click OK.

To create a content page while editing a master page

  • With the master page open in the editor, right-click anywhere on the page and click Add Content Page.

    Visual Web Developer creates a new content page named Defaultx.aspx, where x is a sequential number.

    Note

    To assign a different name to the content page, close it, right-click the name in Solution Explorer, and click Rename.

To create a content page declaratively

  1. Create a new ASP.NET Web page.

  2. Switch to Source view.

  3. Remove everything from the page except the @ Page directive.

  4. Add a MasterPageFile attribute to the @ Page directive that references the master page, as shown in the following code example.

    <%@ Page Language="VB" MasterPageFile="~/Master1.master" %>
    
    <%@ Page Language="C#" MasterPageFile="~/Master1.master" %>
    

    Note

    The @ Page directive might include additional attributes, such as CodeFile and Inherits, which are not shown here.

  5. Add Content controls to the page that correspond to ContentPlaceHolder controls on the master page.

    Note

    A content page can contain only the @ Page directive and Content controls.

Creating Content in the HTML Designer

When you create a content page in Visual Web Developer, the designer creates, by default, a Content control for each ContentPlaceHolder control defined in the master page. You can add custom content to each Content control or explicitly revert to the default content, if any, that is defined in the master page.

To add content to the page in Design view

  • In Design view, drag controls or elements onto the Content control, or click the control and begin typing.

To display default master page content in Design view

  1. Right-click the Content control and click Show Smart Tag.

  2. Click Default to Master's Content.

    Visual Web Developer displays the Content control with the default content defined in the master page. The title bar of the Content control includes the term (Master) to indicate that it is displaying information from the master page, not the content page.

    In this mode, you cannot edit the contents displayed in the Content control.

  3. To create custom content, right-click the control, click Show Smart Tag, and then click Create Custom Control to put the control back into edit mode.

    The designer removes the Content control from the content page's markup.

    Note

    A Content control is required in the content page only to override the corresponding ContentPlaceHolder control from the master page.

See Also

Tasks

Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web Developer

Concepts

ASP.NET Master Pages Overview