Listing 2-1: A Master Page Example |
<%@ master language=”C#” %> <html xmlns=”http://www.w3.org/1999/xhtml”> <head runat=”server”> |
<title>Master Page</title> |
</head> <body> |
<form runat=”server”> |
Master Page Content <br/> <b> |
<asp:ContentPlaceHolder id=”MiddleContent” runat=”server”> </asp:ContentPlaceHolder> |
</b> |
</form> |
</body> </html> |
Apart from looking at the file extension, you can also identify a master file by looking at the new page directive named master at the top of the page. This declarative is used to identify that the current page is a Master Page and prevents the users from requesting the page from a browser. Inside the code, the code contains an element named asp:ContentPlaceHolder that will be used by all the content pages to render appropriate content that is specific to their pages. That’s all there is to creating the Master Page. To create a content page, add a new ASP.NET page named ContentPage.aspx and modify the code to look like the following: |
<%@ page language=”c#” MasterPageFile=”~/BasePage.master” %> <asp:Content id=”Content1” ContentPlaceHolderID=”MiddleContent” |
runat=”server”> Child Page Content |
</asp:Content> |
The code required for the content page is very simple and straightforward. As part of the Page directive, specify a new attribute named MasterPageFile that is used to identify the name of Master Page you want to utilize. This example uses the Master Page created in the previous example. Next you have a new element named asp:Content that is used to associate the asp:ContentPlaceHolder element in the Master Page with the content page. This is done through the use of the ContentPlaceHolderID attribute. That’s all there is to creating a Master Page and using the Master Page from a content page. |
Creating and Sharing Reusable Components in ASP.NET 2.0 |
Prior to ASP.NET 2.0, if you were to reference a reusable class from your ASP.NET application, you had to compile the assembly and place it in the bin folder (or place it in the GAC) of the Web application. But now with ASP.NET 2.0, creating a reusable class is very simple and straightforward. All you need to do is to create the class in a pre-defined subdirectory called App_Code. Any class placed in this directory will be automatically compiled at runtime into a single assembly. This assembly is automatically referenced and will be available to all the pages in the site. Note that you should only put classes in the App_Code subdirectory. |