May 2005 - Posts

One of the biggest features that you have probably heard about in ASP.NET 2.0 is the addition of Master Pages.  Some of you have already worked with them but I figured I should actually discuss them formally since I have not yet.  The Master Pages feature provides the ability to define common structure and interface elements for your site, such as a page header, footer, or navigation bar, in a common location called a "master page", to be shared by many pages in your site. This improves the maintainability of your site and avoids unecessary duplication of code for shared site structure or behavior.
Basically, how it works is that you create a new master page in Visual Studio through the Add New Item menu.  Master pages have a designer just like regular pages.  You can then add all of your common page elements (i.e.: Navigation, footer, etc.).  Then you specify regions in which other pages can substitute content (such as the body, or a subnavigation menu, etc.).  To do this, you create a ContentPlaceHolder control.  The only parameter these controls require are an id.  You can also specify a default for these controls to display if the page using the master page doesn't make use of the place holder.
 
<%-- ContentPlaceHolder control --%>
<asp:contentplaceholder id="BodyContentPlaceHolder" runat="server"/>
 
<%-- ContentPlaceHolder with default content --%>
<asp:contentplaceholder id="BodyContentPlaceHolder" runat="server">
      <div>Default Text Here</div>
</asp:contentplaceholder>
When you create a new content page, it will ask you if you want to select a master page.  When you select a master page it will add the MasterPageFile attribute to your compiler directive.
 
In a content page, all content must be contained inside Content controls.
 
<asp:content id="BodyContent" contentplaceholderid="BodyContentPlaceHolder" runat="server">
    <div>
        Put your page content here.
     </div>
</asp:content>
 
A couple things to note.  Your first master page by default will have a name of site.master.  You can also have master pages which include other master pages.  If you have pages nested inside directories (i.e. / and /foldername) and you are linking to images, etc, ASP.NET adds a new path operator which will default to the application root.  In this case you either have to specify absolute URL paths to your images, or use relative paths with the ~ operator.  ~ will go to the application root instead of the web root.  For example if you had a master page in the blue chip application, ~/images would go to /bluechip/images.
 
Microsoft has finally updated the image library in Visual Studio.
The image libary in Visual Studio hasn't been updated since Visual Basic 6 (and even that was iffy).  Most of the icons look like they are straight out of a Win32 application.  These new images and animations are supposed to reflect more of the Windows XP and Office look.  The library includes animations (AVI and GIF), bitmaps, and icons.  They can be found in a zip file in the Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary folder.  I have also attached the zip file here if you care to take a look at it.
More Posts « Previous page