Extract ContentPlaceHolder (ASP.NET)
- 2 minutes to read
Moves the selected content from a .master page to a new .aspx file and creates a content placeholder that refers to the extracted code block.
After you've created a master page, you may decide that a particular code block shouldn't belong to all pages, and therefore, shouldn't be on the master page. In this instance, you can use Extract ContentPlaceHolder (ASP.NET) to extract this code block to a separate page. This refactoring will also create a content placeholder within the master page, and a reference to it from the extracted code block. So, the appearance of a web page won't change, but it will be much easier for you to dynamically change the portion of the page corresponding to the extracted code block.
- when an ASPX code portion is selected within a .master page.
- The Extract ContentPlaceHolder (ASP.NET) creates a <asp:contentplaceholder> tag at the extraction point. The extracted code is placed into a newly created file inside <asp:content> tags. The extracted content refers to the placeholder by id.
- The newly created file is named NewCodeFile.aspx. If a file with that name already exists, the file is named NewCodeFile1.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.
<%-- 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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <p>My paragraph</p> </div> </form> </body> </html>
<%-- Content file --%> <%@ Page MasterPageFile="NewMasterPageFile0.master" CodeFile="NewCodeFile0.aspx.cs" Language="C#" Inherits="NewCodeFile0.aspx" %> <asp:Content ContentPlaceHolderID="NewContentPlaceHolderId" runat="server"> <p>My paragraph</p></asp:Content>