/* ==ASP.NET MasterPages== If you're unfamiliar with the concept of master pages in ASP.NET 2.0, but are familiar with server-side includes in ASP or PHP or user controls in ASP.NET 1.1, then the concept of master pages will probably make your head spin at first. Probably the most obvious reason for this is because the entire concept of a master page is completely backwards than that of an include and a user control. With master pages, the 'included page' is what is accesses and it pulls itself into a master page where as in an include or a user control model, a page is accesses and it pulls in the included parts. If you are familiar with master pages in desktop publishing applications such as InDesign, then you already know this concept and are well ahead of the game. To setup a master page is rather simple: you create an ASP.NET web form with a '.master' extension and have a "@ Master" page directive instead of a "@ Page" (of course in Visual Studio 2005 you can just add a master page as a new item). Your master page will contain the elements common to the other pages in your web application including your DOCTYPE, the html element, and the head and body elements. So, what isn't provided? Basically any thing you want to be page specific rather than be universally templated can be delegated to a web form "snippet" that uses this master page (this will be demonstrated in a moment). In the place where you want your web form "snippet" to be simply put an ASP.NET 2.0 object called 'ContentPlaceHolder', remembering the ID you gave it as this ID will be used in the web form "snippet" in a moment. Next you simply create an ASP.NET 2.0 web form with the standard "@ Page" page directive, but in addition to the standard 'CodeFile' page directive attribute, you also need to include a page directive attribute named 'MasterPageFile' which points to the ".master" file you previously created. This will link this page to the master page, but to actually make it do something you need to add an ASP.NET 2.0 'Content' control to the page and set it's 'ContentPlaceHolderID' attribute to the ID you used in the 'ContentPlaceHolder' object in the MasterPage. Now any content in the 'Content' object will show up in the MasterPage when *this web form is accessed*. That last part is the key: you access the web form and it knows what it's own master page is. Since multiple pages can share the same MasterPage, the MasterPage doesn't have the first clue about who is using it. As a final note, you can actually use Visual Studio 2005 to create a web form that uses a MasterPage for you automatically. When you go to create a web form, you need to check "Select master page" and this will be done for you. It's important to remember that you *can* have nested master pages. You may elect to use one master page for the overall layout of your system, and then have more master pages that give a particular layout or common content to various sections of your system. The use of master pages in this way should go into your design process at an early stage. Sometimes you will want to use a MasterPage model and other times the situation will demand that you use a user control model. It all depends on your situation. One question I get asked all the time is this: how can I have common information across all my pages? Well, if you are using a master page, then you can simply put properties in your MasterPage and then your individual pages will be able to access it. You do this by accessing the Master property in the page using the MasterPage. It's important to remember however, that you need to cast this property to the type of your MasterPage. Sometimes web developers forget that we are working in an object-oriented system and get too far into procedural thinking. That MasterPage you created has a class name and it inherited from System.Web.UI.MasterPage at some point in the inheritance hierarchy. So, for you to access a property you added to the MasterPage, you need to cast the 'Master' property in your page to the class of the MasterPage you are using. For example, if you have a public property called "UserID" in your MasterPage, you can access it by the following in your page. ((SampleMasterPage)this.Master).UserID */ using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Diagnostics; using Sample; public partial class SampleMasterPage : System.Web.UI.MasterPage { public String UserID { get { return "Sample User"; } } protected void Page_Load(object sender, EventArgs e) { } }