Master Pages in ASP.Net 2.0 - Master Pages – The Concept
(Page 2 of 4 )
A Master Page is more or less just a template, or a shell. In the Master Page, you will define the look and feel across your application, as well as the common controls and functionality that is needed (a date, a login indicator, and so forth). It is possible to define multiple master pages within one site/application. For example, you could have one template for your public website, and a completely separate template for the administrative content management system in the backend. You could even have a Master Page nested within another Master Page, but now that’s just crazy!
A "content" page is the actual page within the site that you’ll be working on, and it will inherit its UI from the Master Page. When the URL of the content page is called, ASP.Net merges the two in the background, and sends the results to the client, who of course will be completely unaware of the fancy process that just took place.
Master Pages – The CodeWorking with Master Pages will offer a seriously difficult learning curve, as we have an outrageously high number of lines of code to learn and remember. In fact, we are tasked to learn all of two new lines of code.
- the first, a new directive, is this:<%@ Master %>
- the second, equally as daunting, is this:<asp:contentplaceholder />
Whew, that was tough! Maybe Microsoft should rethink and create a less complex system…
Okay, enough sarcasm. It’s actually that easy. In fact, here is all the code necessary to get a barebones Master Page up and running:
<%@ Master %>
<html>
<head>
<link rel=”Stylesheet” href=”styles.css” />
</head>
<body>
<img src=”logo.gif” />
<form runat=”server”>
<asp:contentplaceholder runat=”server”
id=”bodyContents”>
<p>default, replaceable content here</p>
</asp:contentplaceholder>
</form>
</body>
</html>
Then you simply save this file with a .master extension, and you’re ready to go! For our example we’ll leave it as the default ‘site.master’.
Worthy to note is that a Master Page in essence is simply an extended UserControl, so you have all of the same attributes available to the @Master directive as you would with the @Control directive.
Next: Content Pages >>
More ASP.NET Articles
More By Justin Cook