Support for master pages in Spring.Web is very similar to upcoming support for master pages in ASP.Net 2.0.
The idea is that user can define a layout template for the site as a master page and specify content placeholders that other pages can reference and populate. Sample master page could look like this:
<%@ Register TagPrefix="spring" Namespace="Spring.Web.UI.Controls" Assembly="Spring.Web" %>
<%@ Page language="c#" Codebehind="Master.aspx.cs" AutoEventWireup="false" Inherits="ArtFair.Web.UI.Master" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Master Page</title>
<link rel="stylesheet" type="text/css" href="<%= Context.Request.ApplicationPath %>/css/styles.css">
<spring:ContentPlaceHolder id="head" runat="server"/>
</head>
<body>
<form runat="server">
<table cellPadding="3" width="100%" border="1">
<tr>
<td colspan="2">
<spring:ContentPlaceHolder id="title" runat="server">
</spring:ContentPlaceHolder>
</td>
</tr>
<tr>
<td>
<spring:ContentPlaceHolder id="leftSidebar" runat="server">
</spring:ContentPlaceHolder>
</td>
<td>
<spring:ContentPlaceHolder id="main" runat="server">
</spring:ContentPlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
As you can see from the code above, master page defines layout for the page and four content placeholders that pages can override. It can also include default content within the placeholder that will be displayed if page does not override specific placeholder.
Page that uses this master page might look like this:
<%@ Register TagPrefix="spring" Namespace="Spring.Web.UI.Controls" Assembly="Spring.Web" %>
<%@ Page language="c#" Codebehind="Child.aspx.cs" AutoEventWireup="false" Inherits="ArtFair.Web.UI.Forms.Child" %>
<html>
<body>
<spring:Content id="leftSidebarContent" contentPlaceholderId="leftSidebar" runat="server">
</spring:Content>
<spring:Content id="mainContent" contentPlaceholderId="main" runat="server">
</spring:Content>
</body>
</html>
Content control in the example above uses contentPlaceholderId property to specify which placeholder from the master page to override. Because this particular page does not define content elements for head and title placeholders, they will be displayed using default content from the master page.
Both ContentPlaceHolder and Content controls can contain any valid ASP.Net markup: HTML, standard ASP.Net controls, user controls...
 | VS.Net 2003 issue
Technically, html and body tags are not necessary in the example above, because they are already defined in the master page. However, if you omit them Visual Studio.Net will complain about a schema and IntelliSense won't work, so it's much easier to work in HTML view if you include them. They will be ignored when the page is rendered. |
Linking child pages to their master
Custom Spring.Web.UI.Page class exposes property called Master that can be used to set reference from a child page to an instance of the master page. There is also property called MasterFile that allows you to specify master page using its file name.
Recommended way to do this is by leveraging Spring IoC container and creating definitions similar to the following:
<?xml version="1.0" encoding="utf-8" ?>
<objects>
<object id="masterPage" type="~/Master.aspx" />
<object id="basePage" abstract="true">
<property name="Master" ref="masterPage"/>
</object>
<object type="Child.aspx" parent="basePage">
</object>
...
</objects>
This approach will allow you to easily change master page you are using for all pages in application. Of course, you can still override master page either for child context or on a page by page basis by specifying value of the Master or MasterFile property directly.
Ted Husted has made available a cookbook example you can play with to get started. Download it here
Thanks Ted!
See also forum thread