Creating a Layout Using Master Pages in ASP.NET?
Thursday, April 3rd, 2008Master Pages can contain markup, controls, or code, or any combination of these elements.
a Master Page can contain a special type of control, called a ContentPlaceHolder control. A ContentPlaceHolder defines a region of the master page rendering that can be substituted with content from a page associated to the master. A ContentPlaceHolder can also contain default content, just in case the derive page does not need to override this content. The syntax of a ContentPlaceHolder control is given below:
<%– ContentPlaceHolder control –%>
<asp:contentplaceholder id=”FlowerText” runat=”server”/>
<%– ContentPlaceHolder with default content –%>
<asp:contentplaceholder id=”FlowerText” runat=”server”>
<h3>Welcome to my florist website!</h3>
</asp:contentplaceholder>
To differentiate a Master Page from a normal page, a Master Page is saved under the .master file extension. A page can derive from a Master Page by defining a MasterPageFile attribute on its Page directive, as demonstrated below. A page that is associated to a Master Page is called a Content Page.
<%@ Page MasterPageFile=”Site.master” %>
A Content Page can declare Content controls that specifically override content placeholder sections in the Master Page. A Content control is associated to a particular ContentPlaceHolder control through its ContentPlaceHolderID property.
<%@ Page MasterPageFile=”Site.master” %>
<asp:content id=”Content1″ contentplaceholderid=”FlowerText” runat=”server”>
With sunshine, water, and careful tending, roses will bloom several times in a season.
</asp:content>
<asp:content id=”Content2″ contentplaceholderid=”FlowerPicture” runat=”server”>
<asp:Image id=”image1″ imageurl=”~/images/rose.jpg” runat=”server”/>
</asp:content>