Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 150 - Default content in contentplaceholder of a master page

Suggested Videos
Part 147 - Master pages in asp.net
Part 148 - Passing data from content page to master page
Part 149 - Passing data from master page to content page



A ContentPlaceHolder in a master page can include default content. 

Let's understand this with an example. The following ContentPlaceHolder in the master page has some default content in an <h1> tag.
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    <h1>Content pages can override this content if they wish to</h1>
</asp:ContentPlaceHolder>



Now right click on the master page and select "Add Content Page". A new content page should be added and would see the following HTML on the content page
<%@ Page Title="" Language="C#" MasterPageFile="~/Site2.Master" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="WebFormsDemo.WebForm5" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

Notice that we have a content control in the content page. Normally we would place the asp.net controls and may be some text inside the content control. However, if we want the content page to default to the content in the ContentPlaceHolder of the master page.
1. Flip the content page to the design mode
2. Right click on the "ContentPlaceHolder" control and select "Default to Master's content"
3. You would get a confirmation dailog box
If you default to the Master Page content, everything in this region will be removed from the page. Do you want to continue?
4. Click Yes

Notice that the default content from the master page "ContentPlaceHolder" is displayed. Now, flip the content page to Source mode, and notice that, the content control is removed. So, if you want a content page to default to master page default content, all you need to do is to remove the content control from the content page.

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.