![]() |
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
Published: Wednesday, January 5, 2005 By Scott Mitchell
Introduction
When building an ASP.NET 1.x Web application, you'll usually use a "header" and "footer" User Control. The header
User Control would contain the starting With ASP.NET 1.x, the options for creating a site with a consistent look and feel are substandard. Yes, you can use User Controls as described above, but you must manually add the standard User Controls to each page. Furthermore, User Controls not rendered in Visual Studio .NET as little gray boxes, thereby removing the WYSIWYG benefits of the VS.NET Designer. A better technique for providing a consistent site layout in ASP.NET 1.x is to use MasterPages. As discussed earlier in Creating Template-Driven Web Sites with MasterPages, MasterPages provide a means to define a common look and feel with editable content regions. A page, then, can utilize a MasterPage, thereby inheriting the standardized look and feel, and leaving the page developer to customize the editable regions. Unfortunately with ASP.NET 1.x there is no support for MasterPages in Visual Studio .NET. Thankfully this has been changed with the upcoming ASP.NET 2.0. In Visual Studio 2005, there is rich, WYSIWYG support for MasterPages. In this article we'll look at MasterPage support in Visual Studio 2005 and ASP.NET 2.0. This article does not delve into the basic premise of MasterPages - for that, refer to Creating Template-Driven Web Sites with MasterPages. Additionally, this article was written during the Beta 1 time frame of ASP.NET 2.0; the screenshots/instructions discussed may or may not make it to the final version of ASP.NET 2.0/Visual Studio 2005. You can download the latest 2.0 beta bits from http://labs.msdn.microsoft.com/.
A Quick MasterPages Overview For example, imagine we wanted to create a site template similar to the one shown in the screenshot below. With this layout, let's assume that the masthead is static, displaying the same message on all pages. However, the main content and menu regions will differ on a page-by-page basis. Keep in mind that with ASP.NET 1.x and Visual Studio .NET our only options for creating such a consistent layout would be to embark on one of the following options:
Creating a MasterPage in Visual Studio 2005
Note that when adding a MasterPage you are asked to select the language for the MasterPage (Visual Basic, C#, or J#). As you might have guessed, MasterPages can support server-side code. You can add code in the Page_Load event handler, for example. This is useful if you want the MasterPage to have some content that should not be editable on a page-by-page basis, but is dynamic nevertheless. For example, you might want to display the name of the currently logged in user in the upper-right hand corner; or you might want to display the number of currently online users in the footer. After creating a new MasterPage, you can add content to the MasterPage through the Designer (shown in the screenshot below) or declaratively. As the screenshot below shows, a new MasterPage contains nothing but a single content placeholder. Recall that the content placeholder is the portion of the MasterPage that will be supplied on a page-by-page basis.
A MasterPage can have multiple content placeholders. To add a new content placeholder in the Designer, simply drag and drop the ContentPlaceholder control from the Toolbox. You can also add a ContentPlaceholder declaratively in the HTML view. ContentPlaceholders, like any other Web control, have a declarative syntax, as shown below:
In order to create a design similar to the one we examined earlier, I crafted the MasterPage to have two content placeholders: one for the main content on the left, and one for the menu on the right. The masthead was added statically to the MasterPage. When all was said an done, I ended up with the MasterPage shown in the screenshot below:
As you can see, the masthead is static and reads "Welcome to My Web Site!" Following that are two content placeholders:
Creating an ASP.NET Page Based on a MasterPage
If you check the "Select master page" checkbox you'll be taken to a dialog box that prompts you to select the MasterPage that
the ASP.NET page should use as a base for its layout. The screenshot below shows this dialog box. Since we have only created
a single MasterPage -
Once you have selected a MasterPage, the new ASP.NET page is created. The content of the ASP.NET page is very minimalistic - rather than having the boilerplate HTML markup that a new ASP.NET page without a MasterPage has, our new ASP.NET page simply contains a reference to the MasterPage selected:
That's all there is in the entire ASP.NET Web page! If you go to the Design view, though, you'll see the entire MasterPage layout. From the Design view you can add controls and markup to the content placeholders, but the markup outside of the content placeholders is dimmed and is not editable through the page. The screenshot below shows the ASP.NET page after some content has been entered into the content placeholders.
After you have added content to the ASP.NET page, when switching back to the HTML view you'll see that new Content controls have been added, containing the controls and markup you specified through the Designer. After typing in some content into both of the content placeholders, the ASP.NET page's declarative syntax looked like:
Notice how using MasterPages separates the layout and the content of the ASP.NET page. The HTML that lays the page out
is separated into the MasterPage; the ASP.NET page itself contains only the page-specific content. What this separation
provides is the ability to alter the entire site's layout and style by just editing the MasterPage. That is, if you want
to alter the text in, say, the masthead, you just need to edit the
Conclusion Happy Programming!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||