Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Using Master Pages in ASP.Net 2.0
By Peter Aitken

Rate This Article: Add This Article To:

Using Master Pages in ASP.Net 2.0
( Page 1 of 2 )

Master pages give you a new way to include common content across multiple pages in ASP 2.0. Their flexibility and ease of use are sure to make them your favorite.

Many web sites follow a plan in which most of the pages have certain content in common, such as a header and footer. As the user navigates from page to page, only the content in the main part of the page changes.

Even the most inexperienced web developer does not need to be told that you do not want to recreate that common content separately for each page. Aside from the extra work involved, any changes to the content would have to be made to each and every page. Bad idea!

Visual Studio provides several options for dealing with this situation, including page inheritance and user controls. A third method, master pages, is the topic of this article.

As the name suggests, a master page is a template that is used to create other pages. It contains whatever content you want present on the derived pages as well as one or more placeholders for the content that changes. You add a new master page to your web site by selecting Website|Add New Item and then selecting Master page from the available templates. An option at this time, which I recommend, is to place code in a separate file — in other words, a code-behind file. Master pages are saved with the .master extension.

A master page starts life containing only one control, a ContentPlaceHolder control. The designer's first task is to define the overall layout of the master page by inserting a table. The row and column structure of the table defines the areas on the page that can contain either static content or dynamic content. For example, for a simple layout with static header and footer and a dynamic content area in between, you would add a table with three rows and one column.

The ContentPlaceHolder control must be cut and pasted into the proper location in the table — in this example, the second row. For more complex layouts you can add additional placeholder controls to the page.

At this point, you are ready to design the master page. It works pretty much the same as designing any ASP.Net page, except that you will not put anything inside the content placeholder controls. You can add ASP and HTML controls, text, images, menus, and so on. The only limitation is that you cannot directly apply an ASP.Net theme to a master page. If you want to use a theme, you must either configure the whole site to use a theme (in the pages element of the ASP.Net settings schema) or define a theme in the content pages.



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Peter Aitken