You need to use master pages along with code-behind in your ASP.NET website. Master pages can be combined with code-behind in the C# language to simplify and ease maintenance of your complex site. Here we look at how to use master pages in ASP.NET and get started with examples.
Master page Stores global page elements that occur on every content page. Extension: .Master Content page Stores page-specific elements that are put into the master. Extension: .aspx Master page code behind Can change master page after it acquires content. Extension: .aspx.cs
First, in the examples, I use the Website project type in Visual Studio. This is recommended as it is a simpler and more streamlined project type. If you are developing a Web Application project, you may need to navigate slightly different menus and dialogs.
The master page serves as a template for the other content pages on the site. The master page has some code-behind methods, and it could auto-generate the page titles for the pages and some H1 headers for each content page. Here are some likely things you will want on your master page.
Navigation and styles. The master page also might contain a TreeView for navigation and a footer with your contact info. It uses markup for the site layout, and contains some CSS styles and JavaScript code. You can use inline CSS in your master page, for an alternative to an external stylesheet.
Images and scripts. Your site's logo is a perfect thing to put in your master page. You can also put code such as Google Analytics, which is implemented as a small piece of JavaScript.
You must go to the Website menu in Visual Studio, and then add a new item. Master Page will appear in that list, so just select it and proceed as normal. You should be familiar enough with Visual Studio to do this quickly.
The ContentPlaceHolder markup tag is where content page content is inserted into each page. So, now we want to make some content pages. How do we do that? Now, look at your new master page file. You will see some tags in the master page. One of the most important ones is as follows:
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
Content pages are made in the exact same way that master pages are made. Go to the Website menu, then add new item, and select Web Form. That's MS-speak for "Content Page". Check the "Select Master Page" checkbox, and finally select your master page.
Next I want to show you an example of a content page and walk you through some parts of it. You should have a content page now and it will have some special markup in it. It will contain several lines of markup similar to the following.
<%@ Page Language="C#" MasterPageFile="~/DotNetPerls.Master" Title="Untitled Page" %> <script runat="server"> </script> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> </asp:Content>
In the master page, you can change various elements picked up from content pages in the code-behind. The master page transforms itself and takes on the properties of the content pages. Then, you can use code in the master pages to change parts of the master page that were retrieved from the content pages.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
public partial class _Default : MasterPage
{
// This code belongs in the *master.cs file. You can usually open this
// by clicking on the little + box next to your *.master file.
protected void Page_Load(object sender, EventArgs e)
{
// Using code-behind, we can modify each page's title according to logic.
// Let's say you want each title on your site to have the words "Your Site: "
// at the start. Try out the following code, and put regular page titles
// in the content pages.
this.Page.Title = "Your Site: " + this.Page.Title;
}
}Here we looked at master pages in the C# programming language and ASP.NET web development framework. We saw how you can dynamically generate pages, leading to fewer typos, with less content to manage. The master page, content page and code-behind model can simplify and streamline your site. Code-behind can separate complex logic from page markup.