Creating Master Page (Default Content) in ASP.NET: Part 2

Introduction

Here is Part 1

We do not have to associate a Content control with every ContentPlaceHolder control contained in a Master Page. We can provide default content in a ContentPlaceHolder control, and the default content will appear unless it is overridden in a particular content page. For example, the Master Page given below includes an additional column, which displays a banner advertisement. The banner advertisement is contained in a ContentPlaceHolder control named Add of.

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<
head id="Head1" runat="server">
    <style type="text/css">
        html
        {
            background-color:silver;
            font:14px Arial,Sans-Serif;
        }
        .content
        {
            margin:auto;
            width:700px;
            background-color:white;
            border:Solid 1px black;
        }
        .leftColumn
        {
            float:left;
            padding:5px;
            width:200px;
            border-right:Solid 1px black;
            height:700px;

        }
        .middleColumn
        {
            float:left;
            padding:5px;
        }
        .rightColumn
        {
            float:right;
            width:auto;
            height:auto;
            border-left:solid 1px black;
            border-bottom:solid 1px black;
            background-color:#eeeeee;
            text-align:center;
        }
        .ad
        {
            margin-top:20px;
        }
        .clear
        {
            clear:both;
        }
    </style>
    <title>Default Content in Master Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div class="content">
<
div class="leftColumn">
            <asp:contentplaceholder
                id="ContentPlaceHolder1"
                runat="server"/>
        </div>
<
div class="middleColumn">
            <asp:ContentPlaceholder
                id="ContentPlaceHolder2"
                runat="server" />
        </div>
<
div class="rightColumn">
            <asp:ContentPlaceHolder
                id="Add_of"
                Runat="server">
                <asp:Image
                    id="imgAd"
                    ImageUrl="~/adds/CSSiteLogo.gif"
                    CssClass="ad"
                    AlternateText="Add of C# CORNER"
                    Runat="server" />
            </asp:ContentPlaceHolder>
        </div>
<
br class="clear" />
</
div>
    </form>
</body>
</
html>

The content page picture given below uses the Master Page. It does not include a Content control that corresponds to the Add_of control in the Master Page. When we open the page in a browser, the default banner advertisement is displayed. Remember that since we have used CSS, the advertisement will automatically be placed according to the amount of text.

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content
    ID="LeftOne"
    ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <b>TITLE</b>
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
</asp:Content>
<
asp:Content
    ID="RightOne"
    ContentPlaceHolderID="ContentPlaceHolder2"
    Runat="Server">
    <b>TITLE</b>
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
    <br />This is my data area.
</asp:Content>

MasterPage-in-vb.net.gif

Note: Continued in the Next Part.

HAVE A GREAT CODING!

Up Next
    Ebook Download
    View all
    Learn
    View all