Vous êtes sur la page 1sur 14

Module 5

Implementing Master Pages and User Controls

Module 5: Implementing Master Pages and User Controls


Creating Master Pages Adding User Controls to an ASP.NET Web Form

Lesson 1: Creating Master Pages


What Are Master Pages? Creating a Master Page

What Are Content Pages?


Creating a Content Page What Are Nested Master Pages?

Runtime Behavior of Master Pages


Adding a Master Page to an Existing Web Application

Project

Discussion: Advantages of Master Pages

What Are Master Pages?

Master pages help you define the overall layout of an ASP.NET application from a single location, the .master file, and then

reuse it in all the content pages that are derived from the master page
Master pages have the following features: The Master directive

ContentPlaceHolder controls, which define the areas where


the replaceable content appears Top-level HTML elements for a page, such as html, head, and form

Creating a Master Page

Navigate to Solution Explorer

Select the required items in the Add New Item dialog box

What Are Content Pages?


Content pages are Web Forms where the layout is based on a master page
Content pages have the following features: The MasterPageFile attribute in Page directive for referencing a master page Page-specific content, all placed in Content controls, mapping to ContentPlaceHolder

controls in master page

Creating a Content Page

Navigate to Solution Explorer

Select the required items in the Add New

Item dialog box

The ContentPlaceHolder controls on the specified master page are automatically referenced by using the Content controls on the new content page

What Are Nested Master Pages?


<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Master pages can Inherits="MasterPage" %> be nested <%@ Master Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="NesterMasterPage.master.cs" create child master pages for partners, Inherits="NesterMasterPage" %>

to

departments, or groups on XHTML a Web site <!DOCTYPE html PUBLIC "-//W3C//DTD <asp:Content ID="Content1" 1.0 Transitional//EN" ContentPlaceHolderID="head" runat="Server"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1</asp:Content> transitional.dtd"> <asp:Content ID="Content2" <html Nested master pages have the following features: ContentPlaceHolderID="ContentPlaceHolder1" xmlns="http://www.w3.org/1999/xhtml"> runat="Server"> <head runat="server"> The .master file extension <asp:ContentPlaceHolder <title></title> ID="ContentPlaceHolder1" runat="server"> <asp:ContentPlaceHolder ID="head" </asp:ContentPlaceHolder> The Master directive runat="server"> </asp:Content> </asp:ContentPlaceHolder> Content controls that map to ContentPlaceHolder controls. In </head> <body> <%@has Page Title="" Language="C#" addition, the child master page its own ContentPlaceHolder <form id="form1" runat="server"> MasterPageFile="~/NesterMasterPage.master" <div> AutoEventWireup="true" controls. <asp:ContentPlaceHolder CodeFile="Default2.aspx.cs" ID="ContentPlaceHolder1" runat="server"> Inherits="Default2" %> </asp:ContentPlaceHolder> </div> You can view nested master pages in <asp:Content the Design ID="Content1" view in </form> ContentPlaceHolderID="ContentPlaceHolder1" </body> Runat="Server"> Visual Studio 2008 SP1 </html> </asp:Content>

Runtime Behavior of Master Pages


The content of the The of the Thecontent user requests individual Content individual Content a page by typing the controls is merged into controls is merged URL of the content the corresponding into the corresponding page. ContentPlaceHolder ContentPlaceHolder controls on the master controls in reads the master ASP.NET the page page. directive of the Page content page. If the ASP.NET renders directive contains a the final merged MasterPageFile page to the browser attribute, ASP.NET browser. retrieves the master page. ASP.NET merges the master page content into the control tree of the content page.

Content Page ContentPlaceHolder MasterPageFile

Lesson 2: Adding User Controls to an ASP.NET Web Form


What Are User Controls? Advantages and Disadvantages of Using User Controls

Converting a Web Form to a User Control


Demonstration: How To Convert a Web Form to a User

Control

Adding a User Control to a Web Form

What Are User Controls?


User controls are ASP.NET pages with an .ascx file extension

Features of user controls: Use the Control directive Consist of a user-defined Web server control Simplify the reuse of code and UI components in a Web application Do not contain the top-level HTML elementshtml, body, or form

Can contain static markup and server controls


Contain code to handle its own events

Advantages and Disadvantages of Using User Controls


Advantages
User controls are self-contained User controls can be used multiple times User controls can be written in a User interface and code can get duplicated Code is visible

Disadvantages

different language from the


language used for the main hosting page for Web sites User controls can be shared in a Web application

User controls are typically used for creating small units of layout and code that are used repeatedly within a Web application, such as login, validation, navigation, toolbars, and similar functionality

Converting a Web Form to a User Control

Remove all top-level HTML elements Change the Page directive to the Control directive Add a ClassName attribute to the Control directive Change the file extension from .aspx to .ascx Change the class type to UserControl Add the properties

Module Review and Takeaways


Review Questions Real-World Issues and Scenarios

Best Practices

Vous aimerez peut-être aussi