Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Using the Master Page

Using the Master Page

It’s time for our moment of glory, when we assemble all the pieces we’ve been building and put them to work! We’ll start by re-creating the Default.aspx web form, but this time, we’ll use the master page. Start by deleting your current Default.aspx file by right-clicking that file in Solution Explorer, and choosing

Delete. You’ll be warned that Default.aspx is about to be deleted (see Figure 5.41)—choose OK.

Figure 5.41. Deleting Default.aspx

Click the root node in Solution Explorer, then go to File > New File… (or rightclick the root node in Solution Explorer and select Add New Item… from the context menu).

In the dialog that appears, choose the Web Form template, leave the default name of Default.aspx as is, and make sure both the Place code in separate file and Select master page checkboxes are checked, as shown in Figure 5.42.

Figure 5.42. Creating the new Default.aspx

199

Chapter 5: Building Web Applications

Once you click Add, you’ll be asked to select the master page you want to use.

Choose Dorknozzle.master, and click OK.

Our new form inherits everything from its master page, so its code is minimal:

Visual Basic

File: Default.aspx (excerpt)

<%@ Page Language="VB" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

</asp:Content>

This file is almost exactly the same when it’s written in C#—the only differences are the Language, AutoEventWireup, and CodeFile attributes in the Page directive. Let’s modify the file by adding some content to the ContentPlaceHolder, and altering the page title. Edit the file to reflect the highlighted sections here:

File: Default.aspx (excerpt)

<%@ Page Language="VB" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Welcome to Dorknozzle!" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<h1>Company News</h1>

<p>We'll add some news later.</p> <h1>Company Events</h1>

<p>We'll add company events later.</p>

</asp:Content>

Switch to Design View to see a preview of the whole page, like the one shown in Figure 5.43. The master page areas will be grayed out, and only the content placeholder will be editable.

By default, when you select Debug > Start Debugging or press F5, Visual Web Developer executes the page that’s being edited. However, if you prefer, you can set a particular page to execute whenever you start debugging. To make sure that Default.aspx is the page that’s loaded when the project is executed, right-click Default.aspx in Solution Explorer, and select Set As Start Page.

Now, execute Default.aspx by hitting F5. It should appear as per the page in Figure 5.44, with all the CSS applied.

200