This tutorial will demonstrate how to nest master pages within other master pages using ASP.NET 4.0 and C#.
Adding the Main Master Page
To demonstrate nesting master pages we will need to create two master pages, a main master page and a sub master page. Then, we will derive a content page from the sub master page to demonstrate how the pages all link together. At this point I have created a new ASP.NET Empty Web Site. To begin:
This is the going to be the main page that we derive the other pages from. When you created the page, it setup a content place holder for us named 'ContentPlaceHolder1'. We need to change that to 'MainMasterContent'. Also, above the content place holder I am going to add the following text 'Main Master Page' and set it to bold with a few break lines after it. The code for the MainMasterPage.master should look like this:
Adding the Sub Master Page
The next thing we need to do is add in another master page that will actually be encapsulated in the main master page. To do this:
We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud servers!
Adding the Content Page
Finally, we need to add a page that inherits from our SubMasterPage which inherits from the MainMasterPage. To do this:
Testing
To actually test this out and see what this has done, load up the web site. Notice you see a page that contains the content from our main master page, then sub master page, then the Default.aspx page that looks something like this:
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
What is happening here is that our main master page contains a content place holder in which the content from the sub master page is added. Likewise, the sub master page contains a content place holder in which the content from the Default.aspx page is added. Furthermore, each master page can contain its own content such as the headers for each master page in this example.
Thank you for reading this tutorial, check us out for more on Master Pages.
To demonstrate nesting master pages we will need to create two master pages, a main master page and a sub master page. Then, we will derive a content page from the sub master page to demonstrate how the pages all link together. At this point I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add new item...
- Select a master page.
- Name it 'MainMasterPage.master'.
- Click add.
This is the going to be the main page that we derive the other pages from. When you created the page, it setup a content place holder for us named 'ContentPlaceHolder1'. We need to change that to 'MainMasterContent'. Also, above the content place holder I am going to add the following text 'Main Master Page' and set it to bold with a few break lines after it. The code for the MainMasterPage.master should look like this:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMasterPage.master.cs" Inherits="MainMasterPage" %> |
Adding the Sub Master Page
The next thing we need to do is add in another master page that will actually be encapsulated in the main master page. To do this:
- Right click the project in your solution explorer.
- Select add new item...
- Select a master page.
- Name it 'SubMasterPage.master'.
- Ensure that the Select master page option is checked.
- Click add.
- Select MainMasterPage.master.
- Click ok.
<%@ Master Language="C#" MasterPageFile="~/MainMasterPage.master" AutoEventWireup="true" CodeFile="SubMasterPage.master.cs" Inherits="SubMasterPage" %> |
We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud servers!
Adding the Content Page
Finally, we need to add a page that inherits from our SubMasterPage which inherits from the MainMasterPage. To do this:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Ensure that the Select master page option is checked.
- Click add.
- Select SubMasterPage.master.
- Click ok.
<%@ Page Title="" Language="C#" MasterPageFile="~/SubMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
Testing
To actually test this out and see what this has done, load up the web site. Notice you see a page that contains the content from our main master page, then sub master page, then the Default.aspx page that looks something like this:
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
What is happening here is that our main master page contains a content place holder in which the content from the sub master page is added. Likewise, the sub master page contains a content place holder in which the content from the Default.aspx page is added. Furthermore, each master page can contain its own content such as the headers for each master page in this example.
Thank you for reading this tutorial, check us out for more on Master Pages.
No comments:
Post a Comment