Flylib.com

Books Software

 
 
 

ASP.NET 2.0 Revealed - page 29


Summary

Wow, that's a lot of new stuff, right? The new concepts introduced in this chapter will totally change the way you handle data within ASP.NET. Thanks to Data Source controls, it's so much simpler now to connect a page with a database. One of the biggest benefits is that you can easily build your own providers to access your custom data sources as well.



Chapter 4: Working with Master Pages

Overview

The Master Pages functionality goes by many names , one of which is "page templating." All of these expressions have the same meaning. They refer to the central deposit of site layouts, including the navigation, which will then automatically be transferred to single Content Pages. From now on, you have to make changes to the general layout in just one place. Those changes will become effective on all corresponding pages that use the Master Page you changed. Sounds like a standard requirement for approximately 100% of all web sites, don't you agree? Right!

The 1.0 and 1.1 versions of ASP.NET didn't offer the Master Pages functionality. That was a good reason for many developers to implement their own approaches. These approaches went from the static output of HTML tags to the implementation of user controls to the drilling of page life cycles. The latter offered the greatest flexibility and was my favorite too. I have to admit, however, that it wasn't an ideal solution.

With version 2.0, ASP.NET offers its own and fully integrated support for Master Pages for the first time. You develop one or several layouts as Master Pages and allocate them to single Content Pages. In combination with VS .NET, you can now design the Content Pages visually within the Master Pages context and fill one or several defined placeholders.



Creating a New Master Page

Please create a new and empty web site before you start with a new Master Page. Then choose Add New Item from the Website menu and establish a new element from the type MasterPage with the name MasterPage1.master. All Master Pages have this special ending, which helps you avoid starting the page directly within the browser.

In the result, you get a new page that includes a control of type ContentPlaceHolder. This is a regular control, which means that you can integrate it as usual into the HTML layout of the page. Now I would like to show you a table to which I've added some text. I also placed the control in one of the cells , as you can see in Figure 4-1.

click to expand
Figure 4-1: Design your site any way you want!

Have a look at the source code of the page in Listing 4-1 and you can see that the structure of the Master Page is compatible with a normal web site. The ContentPlaceHolder control is positioned just like a normal server control. There is still one difference, however: the @Master directive. In this case, it replaces the @Page directive and enables you to place default values for several attributes. If you set attribute values for @Master and don't overwrite them in your @Page directive, you adopt them. This shows clearly that the setting of the page has priority.

Listing 4-1: The Master Page Source Code

start example
<%@ master language="C#" %> <script runat="server"> </script> <html> <head runat="server"> <title>Untitled Page</title> </head> <body> <form runat="server"> <table id="Table1" cellspacing="1"

cellpadding

="1" width="100%" border="1"> <tr> <td colspan="2"> <h1>My Little Company</h1> </td> </tr> <tr> <td>Navigation?</td> <td> <asp:contentplaceholder id="ContentPlaceHolder1"

runat

="server"> </asp:contentplaceholder> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form> </body> </html>
end example