Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

Create Master Pages in ASP.NET 2.0 (Continued)

This works fine in Design mode for the master page, but the image won't be found if your master pages are in a different location than content pages, because the relative path starts at the content page location and not the master page location when ASP.NET 2.0 begins compiling the page. If you take a look at the content page linked to this master page in Design mode, you'll see that the image can't be found.

You could overcome this problem by using ASP.NET server controls instead of HTML controls or by using the complete physical address. However, this can cause problems when moving the application from development to production. You could also make the image control a server control and use the special "~" character used by ASP.NET during page rendering to represent the home location of the current application:

<img src="~/Images/AG00004_.GIF" 
   runat=server />
ADVERTISEMENT

The drawback to this approach is that the images won't display in the IDE, because it appears that the IDE doesn't translate the "~" within HTML controls in Design mode in beta 1. However, notice that the IDE does translate this character correctly inside the <%@ Page %> and <%@ Master %> declarations.

Once you've laid out the master page in Design mode, you can manage events in the code-behind file just like a normal ASP.NET page. You can manipulate all of the properties of the master page as well as respond to any page events, or events for controls placed inside of the master page. Opening up the code-behind file for the first time on a master page, you'll see code that looks pretty much like your normal ASP.NET page code:

Partial Class BasicMasterPage_master
   Sub Page_Load (ByVal sender As Object, _
      ByVal e As System.EventArgs) _
      Handles Me.Load
   End Sub
End Class

Now that you've defined a master page, it's time to create a Web application page that will inherit from the master page (called a content page). Go to Add | New Item in VS.NET 2005 and select Web Form. Make sure that the Select Master Page checkbox is checked so that you can visually select the master page (see Figure 2). You can also select the master page or change it later by altering the new MasterPageFile attribute (see Listing 2 for a sample content page created using the BasicMasterPage.master file in the sample project provided). Notice that the page declaration is just like the one you're probably used to already, except that there is now a MasterPageFile property that defines the link between this content page and the master page:

<%@ Page Language="VB"
   MasterPageFile=
      "~/MasterPages/BasicMasterPage.master" 
   AutoEventWireup="false" 
      CompileWith="BasicContentPage.aspx.vb" 
   ClassName="BasicContentPage_aspx" 
      title="Basic Content Page" %>

Remember that the master page also had a <TITLE> property defined inside the HTML <BODY> tag. If you don't define a title property within the content page declaration, the title property of the master page will be used for the title of the rendered ASP.NET page.

The only control at the top of the ASPX page is an <asp:content> control. This control is linked to the <asp:contentplaceholder> in the master page through the ContentPlaceHolderID property. VS.NET 2005 links it automatically if you select the master page through the IDE. The VS.NET 2005 IntelliSense has also been improved so that when adding or modifying this property, you can select any of the content placeholders defined in the master page.

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home