How to Create ASP.NET MasterPages Content PlaceHolders
See more tutorials in Controls. This post has Comments Off on How to Create ASP.NET MasterPages Content PlaceHolders.
In this tutorial we will learn how to use MasterPages to setup Content PlaceHolders in ASP.NET. A Content PlaceHolder is an area designated in a MasterPage that is virtually empty, but it may be positioned in a spot so that you don’t have to do this in the front-end. The Content PlaceHolder is accessed and filled in the front-end, but usually it is positioned in the MasterPage so that you can keep all of your similar sites uniformed.
Step 1. Creating the PlaceHolders
ASP Content PlaceHolders are very simple to setup and use and they can make a website much easier to build with the help of MasterPages.
1. Create a new Web Form called “Default.aspx”
2. Create a MasterPage called “Master.Master”
3. Inside the MasterPage is where we create our Content PlaceHolders, the code below is a sample of an empty placeholder.
<asp:ContentPlaceHolder ID=“Section1” runat=“server”>
4. You will notice that the placeholder has an ID tag. This is how we can identify it to fill it with data form the aspx page.
5. Go back to the “default.aspx” page and add this to the very top line to let this file know what master page to use: Inherits=”MasterPageContent.Master”
6. Now that we have a Content Placeholder and we are linked to the MasterPage we can go to the “default.aspx” page and fill the Content PlaceHolder with data.
Step 2. Using the PlaceHolders
1. Go into the default.aspx page and insert a content Tag like below. Notice we specify a ContentPlaceHolderID, this is how it knows which Content PlaceHolder to fill.
<asp:Content runat=“server” ID=“content1” ContentPlaceHolderID=“Section1”>
In this tutorial we learned how to create a Content PlaceHolder in a Master Page and use it in a Web Form. Content PlaceHolders are the basis of Master Pages and they are very helpful in placing and organizing items in several web forms in similar positions. With the use of MasterPages your website should certainly be looking better in no time.