Skip to main content

Extract ContentPlaceHolder and Create Master (ASP.NET)

  • 2 minutes to read

Creates a new master page using the code outside of the selection.

#Purpose

If you’ve created a page that you feel would be great to use as a template for other pages, the Extract ContentPlaceHolder and Create Master (ASP.NET) will be of great help to you. You simply select the content that you want to be changed dynamically, and apply this refactoring. All the code outside the selected block (the template) will be moved to a newly created .master page that will have a content placeholder at the extraction point. The selected code block will remain within the same page and will refer to the content placeholder. As a result, the operation won’t change the page’s appearance, but will make it much easier to dynamically change the extracted content.

#Availability

Available from the context menus or via shortcuts:

  • when an ASPX code portion is selected within a .aspx file.

#Notes

  • Extract ContentPlaceHolder and Create Master (ASP.NET) surrounds the initially selected code block with <asp:content> tags. The content outside the selection is moved to a new file and <asp:contentplaceholder> tag is placed at the extraction point. The Extracted content refers to the placeholder by id.
  • The newly created file is named NewMasterPageFile.master. If a file with this name already exists, the file is named NewMasterPageFile1.aspx, etc.
  • Rename is automatically activated for the content placeholder id so you can provide a meaningful string. Note that a reference to this id from the extracted code block is also updated.
  • If you haven’t changed the placeholder’s id right away, you can invoke Rename for it later.

#Example

<%-- .aspx file --%>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <p>My paragraph</p>
    </div>
    </form>
</body>
</html>

Result:

<%-- .master file --%>

<%@ Master CodeFile="NewMasterPageFile0.master.cs" Language="C#" Inherits="NewMasterPageFile0" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:ContentPlaceHolder id="NewContentPlaceHolderId" runat="server">
</asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

#Screenshot

rsExtractContentPlaceHolderAndCreateMasterPage