ASP.NET Tutorial/Development/Master page

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A Master Page enables you to share the same content among multiple content pages.

   <source lang="csharp">

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       html
       {
           background-color:silver;
           font:14px Arial,Sans-Serif;
       }
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           border:Solid 1px black;
       }
       .leftColumn
       {
           float:left;
           padding:5px;
           width:200px;
           border-right:Solid 1px black;
           height:700px;
       }
       .rightColumn
       {
           float:left;
           padding:5px;
       }
       .clear
       {
           clear:both;
       }
   </style>
   <title>Simple Master</title>

</head> <body>

   <form id="form1" runat="server">
           <asp:contentplaceholder
               id="ContentPlaceHolder1"
               runat="server"/>
           <asp:contentplaceholder
               id="ContentPlaceHolder2"
               runat="server"/>
       
   </form>

</body> </html>

The content page uses the Master Page, Default.master. The Master Page is associated through the MasterPageFile attribute. This attribute contains the virtual path to a Master Page. You must place all the content contained in a content page within the Content controls. Otherwise, you get an exception. File: SimpleContent.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" %> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content in the first column
   
Content in the first column
Content in the first column
Content in the first column
Content in the first column

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Content in the second column
   
Content in the second column
Content in the second column
Content in the second column
Content in the second column

</asp:Content></source>


Associate different Master Pages dynamically with a content page

   <source lang="csharp">

File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" %> <script runat="server">

   protected void Page_PreInit(object sender, EventArgs e)
   {
       if (Request["master"] != null)
       {
           switch (Request["master"])
           {
               case "Dynamic1":
                   this.MasterPageFile = "Default.master";
                   break;
               case "Dynamic2":
                   this.MasterPageFile = "MasterDefault.master";
                   break;
           }
       }
       
   }

</script> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Select a Master Page:
  • <a href="Default.aspx?master=Default">Dynamic Master 1</a>
  • <a href="Default.aspx?master=MasterDefault">Dynamic Master 2</a>

</asp:Content></source>


Change master page in page init event

   <source lang="csharp">

File: Default.aspx <%@ Page Language="C#"

        MasterPageFile="~/Default.master" 
        AutoEventWireup="true" 
        CodeFile="DynamicContentPage.aspx.cs" 
        Inherits="DynamicContentPage_aspx" 
        Title="Untitled Page" %>

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

File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class DynamicContentPage_aspx : System.Web.UI.Page {

 protected void Page_PreInit(object sender, EventArgs e)
 {
   this.MasterPageFile = "TableMaster.master";
 } 

}

File: Default.master <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="SiteTemplate" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
       </asp:contentplaceholder>
This is a simple footer.
   </form>

</body> </html>

File: Default.master.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class SiteTemplate : System.Web.UI.MasterPage {

   protected void Page_Load(object sender, EventArgs e)
   {
   }

}

File: TableMaster.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="TableMaster" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">

My Header

           <asp:treeview ID="Treeview1" runat="server" Width="150px">
               <Nodes>
                   <asp:TreeNode Text="Root" Value="New Node">
                       <asp:TreeNode Text="Page 1" Value="Page 1"></asp:TreeNode>
                       <asp:TreeNode Text="Page 2" Value="Page 2"></asp:TreeNode>
                   </asp:TreeNode>
               </Nodes>
           </asp:treeview>
           <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
           </asp:contentplaceholder>
           My Footer
   </form>

</body> </html>

File: TableMaster.master.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class TableMaster : System.Web.UI.MasterPage {

   public bool ShowNavigationControls
 {
   set { Treeview1.Visible = value; }
   get { return Treeview1.Visible; }
 }

}</source>


Deal with the relative path between master page and content page

   <source lang="csharp">

use the Page.ResolveUrl() method to translate an application relative URL into an absolute URL. File: MasterPages\ImageMaster.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Image Master</title>

</head> <body>

   <form id="form1" runat="server">
   <img src="<%=Page.ResolveUrl("~/MasterPages/Logo.gif") %>" alt="Website Logo" />
   <asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
   </form>

</body> </html></source>


Dynamically change master page

   <source lang="csharp">

File: Default.aspx <%@ Page Language="C#" MasterPageFile="Simple1.master" AutoEventWireup="true"

   CodeFile="Default.aspx.cs" 
   Inherits="Default" 
   Title="Dynamic Masters"

%> <asp:Content ID="Content1" ContentPlaceHolderID="ContentOfThePage" runat="server">

This content page is currently bound to <% =this.MasterPageFile %>.

</asp:Content> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {

   protected void Page_PreInit(object sender, EventArgs e)
   {
   MasterPageFile = "simple2.master";
   }

} File: simple1.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Hello, master pages</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Panel ID="HeaderPanel" runat="server" BorderStyle="Outset"
           BorderWidth="1px" Height="50px" Width="100%">
           <asp:Label style="margin:5px;" ID="TitleBox" runat="server" EnableTheming="False" Font-Bold="True" Font-Names="Impact"
               Text="Programming ASP.NET 3.5" Font-Size="XX-Large" ForeColor="Yellow" Width="100%"></asp:Label>
       </asp:Panel>
       <asp:contentplaceholder id="ContentOfThePage" runat="server">
       </asp:contentplaceholder>
       <asp:Panel ID="Panel1" runat="server" BorderStyle="Outset"
           BorderWidth="1px" Width="100%" HorizontalAlign="Right">
           <asp:Label style="margin:8px;" ID="SubTitleBox" runat="server" EnableTheming="False" 
               Font-Bold="True" Font-Names="Lucida Console"
               Text="Dino Esposito" Font-Size="Large" ForeColor="Orange"></asp:Label>
       </asp:Panel>        
   </form>

</body> </html> File: simple2.master <%@ Master Language="C#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Hello, master pages</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Panel ID="HeaderPanel" runat="server" BorderStyle="Outset"
           BorderWidth="1px" Height="50px" Width="100%">
           <asp:Label style="margin:5px;" ID="TitleBox" runat="server" EnableTheming="False" Font-Bold="True" Font-Names="Impact"
               Text="Programming ASP.NET 3.5" Font-Size="XX-Large" ForeColor="Yellow" Width="100%"></asp:Label>
       </asp:Panel>
       <asp:contentplaceholder id="ContentOfThePage" runat="server">
       </asp:contentplaceholder>
       <asp:Panel ID="Panel1" runat="server" BorderStyle="Outset"
           BorderWidth="1px" Width="100%" HorizontalAlign="Right">
           <asp:Label style="margin:8px;" ID="SubTitleBox" runat="server" EnableTheming="False" 
               Font-Bold="True" Font-Names="Lucida Console"
               Text="Dino Esposito" Font-Size="Large" ForeColor="Orange"></asp:Label>
       </asp:Panel>        
   </form>

</body> </html></source>


Exposing Master Page Properties

   <source lang="csharp">

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">

   public string BodyTitle
   {
       get { return ltlBodyTitle.Text; }
       set { ltlBodyTitle.Text = value; }
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       html
       {
           background-color:silver;
       }
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           padding:10px;
       }
       h1
       {
           border-bottom:solid 1px blue;
       }
   </style>
   <title>Property Master</title>

</head> <body>

   <form id="form1" runat="server">

<asp:Literal ID="ltlBodyTitle" runat="server" />

   <asp:contentplaceholder
       id="ContentPlaceHolder1"
       runat="server" />
   </form>

</body> </html>

File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" %> <%@ MasterType VirtualPath="~/Default.master" %> <script runat="server">

   void Page_Load()
   {
       if (!Page.IsPostBack)
       {
           Master.BodyTitle = "The Body Title";
       }
   }

</script> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content, Content, Content, Content
   
Content, Content, Content, Content
Content, Content, Content, Content
Content, Content, Content, Content
Content, Content, Content, Content

</asp:Content></source>


How Master Pages and Content Pages Are Connected

   <source lang="csharp">

A master page starts with a Master directive that specifies the same information. <%@ Master Language="C#"

          AutoEventWireup="true" 
          CodeFile="Default.master.cs"
          Inherits="SiteTemplate" %>
          

The ContentPlaceHolder is like any ordinary control. <%@ Master Language="C#"

          AutoEventWireup="true" 
          CodeFile="Default.master.cs"
          Inherits="SiteTemplate" %>

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

   <title>Untitled Page</title>

</head> <body> <form id="form1" runat="server"> >
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> This is a simple footer. </form> </body> </html> ASP.NET links your page to the master page with attribute named MasterPageFile <%@ Page Language="C#"

        MasterPageFile="~/Default.master"
        AutoEventWireup="true" 
        CodeFile="SimpleContentPage.aspx.cs"
        Inherits="SimpleContentPage" 
        Title="Untitled Page" %>
        

path ~/ is to specify the root website folder. The Page directive has another new attribute Title. Title attribute overrides the title specified in the master page. The content page can¡�t define anything provided in the master page,

 including the <head> section, 
 the root <html> element, 
 the <body> element, and so on. 
 

The content page supply a Content tag that corresponds to the ContentPlaceHolder in the master page. <%@ Page Language="C#"

        MasterPageFile="~/Default.master"
        AutoEventWireup="true" 
        CodeFile="SimpleContentPage.aspx.cs"
        Inherits="SimpleContentPage" 
        Title="Content Page" %>

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

   Here"s some new content!


</asp:Content> ContentPlaceHolderID attribute in the <Content> tag must match the ContentPlaceHolder specified in the master page exactly.</source>


Loading Master Pages Dynamically for Multiple Content Pages

   <source lang="csharp">

After registering the DynamicMasterPage class, every page in your application automatically inherits from the new base class. Every page inherits the new OnPreInit() method and every page loads a Master Page dynamically.

File: DynamicMasterPage.cs using System; using System.Web.UI; using System.Web.Profile; public class DynamicMasterPage : Page {

   protected override void OnPreInit(EventArgs e)
   {
       this.MasterPageFile = (string)Context.Profile["MasterPageFile"];
       base.OnPreInit(e);
   }

} After you create a new base Page class, you need to register it in the web configuration file. <configuration>

 <system.web>
   <pages pageBaseType="DynamicMasterPage" />
   <profile>
     <properties>
       <add
         name="MasterPageFile"
         defaultValue="Dynamic1.master" />
     </properties>
   </profile>
 </system.web>

</configuration></source>


Master page with default content

   <source lang="csharp">

File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Untitled Page" %>

File: Default.master

<%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       this is a test
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> This is default content.
</asp:contentplaceholder> This is a simple footer.
   </form>

</body> </html></source>


Modifies the Master Page <meta> tags (the tags used by search engines when indexing a page).

   <source lang="csharp">

You receive a NullReference exception if you use the Page.Header property when the Master Page does not contain a server-side <head> tag. File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       html
       {
           background-color:silver;
           font:14px Arial,Sans-Serif;
       }
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           border:Solid 1px black;
       }
       .leftColumn
       {
           float:left;
           padding:5px;
           width:200px;
           border-right:Solid 1px black;
           height:700px;
       }
       .rightColumn
       {
           float:left;
           padding:5px;
       }
       .clear
       {
           clear:both;
       }
   </style>
   <title>Simple Master</title>

</head> <body>

   <form id="form1" runat="server">
           <asp:contentplaceholder
               id="ContentPlaceHolder1"
               runat="server"/>
           <asp:contentplaceholder
               id="ContentPlaceHolder2"
               runat="server"/>
       
   </form>

</body> </html> File: Default.aspx

<%@ Page Language="C#" MasterPageFile="~/Default.master" %> <script runat="server">

   void Page_Load()
   {
       HtmlMeta metaDesc = new HtmlMeta();
       metaDesc.Name = "DESCRIPTION";
       metaDesc.Content = "A sample of using HtmlMeta controls";
       HtmlMeta metaKeywords = new HtmlMeta();
       metaKeywords.Name = "KEYWORDS";
       metaKeywords.Content = "HtmlMeta,Page.Header,ASP.NET";
       HtmlHead head = (HtmlHead)Page.Header;
       head.Controls.Add(metaDesc);
       head.Controls.Add(metaKeywords);
   }

</script>

<asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content in the first column
   
Content in the first column
Content in the first column
Content in the first column
Content in the first column

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Content in the second column
   
Content in the second column
Content in the second column
Content in the second column
Content in the second column

</asp:Content></source>


Modify a control in a Master Page by using the FindControl() method in a content page.

   <source lang="csharp">

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           padding:10px;
       }
       h1
       {
           border-bottom:solid 1px blue;
       }
   </style>
   <title>Find Master</title>

</head> <body>

   <form id="form1" runat="server">

<asp:Literal ID="ltlBodyTitle" runat="server" />

   <asp:contentplaceholder
       id="ContentPlaceHolder1"
       runat="server" />
   </form>

</body> </html> File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" %> <script runat="server">

   void Page_Load()
   {
       if (!Page.IsPostBack)
       {
           Literal ltlBodyTitle = (Literal)Master.FindControl("ltlBodyTitle");
           ltlBodyTitle.Text = "The Body Title";
       }
   }

</script> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content, Content, Content, Content
   
Content, Content, Content, Content
Content, Content, Content, Content
Content, Content, Content, Content
Content, Content, Content, Content

</asp:Content></source>


Multipart master page

   <source lang="csharp">

File: Default.aspx

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

   This is the generic content for this page. 

this is a test.</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="OtherLinksContent" Runat="Server">

   Here"s a <a href="http://www.nfex.ru">link</a>.
   
Here"s a <a href="http://www.nfex.ru">link</a>.
Here"s a <a href="http://www.nfex.ru">link</a>.
Here"s a <a href="http://www.nfex.ru">link</a>.</asp:Content>

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
           <img src="http://www.nfex.ru/style/logo.png" alt="nfex.ru" />

<asp:contentplaceholder id="MainContent" runat="server"> </asp:contentplaceholder>
               
                   OTHER LINKS
<asp:ContentPlaceHolder id="OtherLinksContent" runat="server"> </asp:ContentPlaceHolder>
           This is a simple footer.
   </form>

</body> </html></source>


Nesting Master Pages

   <source lang="csharp">

A single site-wide Master Page that applies to all the content pages. Aection-wide Master Pages that apply to only the pages contained in a particular section. File: Site.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       .content
       {
           margin:auto;
           border-style:solid;
           background-color:white;
           padding:10px;
       }
       .tabstrip
       {
           padding:3px;
           border-top:solid 1px black;
           border-bottom:solid 1px black;
       }
       .tabstrip a
       {
           font:14px Arial;
           color:DarkGreen;
           text-decoration:none;
       }
       .column
       {
           float:left;
           padding:10px;
           border-right:solid 1px black;
       }
       .rightColumn
       {
           float:left;
           padding:10px;
       }
       .clear
       {
           clear:both;
       }
   </style>
   <title>Site Master</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Image
           id="imgLogo"
           ImageUrl="~/Images/SiteLogo.gif"
           AlternateText="Website Logo"
           Runat="server" />
       <asp:HyperLink
           id="lnkProducts"
           Text="Products"
           NavigateUrl="~/Products.aspx"
           Runat="server" />
        
       <asp:HyperLink
           id="lnkServices"
           Text="Services"
           NavigateUrl="~/Services.aspx"
           Runat="server" />
       <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
       </asp:contentplaceholder>
       
copyright © 2007 by the Company
   </form>

</body> </html> File: SectionProducts.master <%@ Master Language="C#" MasterPageFile="~/Site.master" %> <asp:Content

   id="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="server">
       <asp:ContentPlaceHolder
           id="ContentPlaceHolder1"
           Runat="server" />
       <asp:ContentPlaceHolder
           id="ContentPlaceHolder2"
           Runat="server" />
       <asp:ContentPlaceHolder
           id="ContentPlaceHolder3"
           Runat="server" />

</asp:Content>

File: SectionServices.master <%@ Master Language="C#" MasterPageFile="~/Site.master" %>

<asp:Content

   id="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="server">
       <asp:ContentPlaceHolder
           id="ContentPlaceHolder1"
           Runat="server" />
       <asp:ContentPlaceHolder
           id="ContentPlaceHolder2"
           Runat="server" />

</asp:Content>

File: Products.aspx <%@ Page Language="C#" MasterPageFile="~/SectionProducts.master" %> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Products, Products, Products
   
Products, Products, Products
Products, Products, Products
Products, Products, Products
Products, Products, Products

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Products, Products, Products
   
Products, Products, Products
Products, Products, Products
Products, Products, Products
Products, Products, Products

</asp:Content> <asp:Content

   ID="Content3"
   ContentPlaceHolderID="ContentPlaceHolder3"
   Runat="Server">
   Products, Products, Products
   
Products, Products, Products
Products, Products, Products
Products, Products, Products
Products, Products, Products

</asp:Content> File: Services.aspx <%@ Page Language="C#" MasterPageFile="~/SectionServices.master" Title="Services" %>

<asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Services, Services, Services
   
Services, Services, Services
Services, Services, Services
Services, Services, Services
Services, Services, Services

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Services, Services, Services, Services, Services
   
Services, Services, Services, Services, Services
Services, Services, Services, Services, Services
Services, Services, Services, Services, Services
Services, Services, Services, Services, Services

</asp:Content></source>


Simplest Master page

   <source lang="csharp">

File: Default.master <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="SiteTemplate" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <img src="apress.jpg" alt="Logo" />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder>
This is a simple footer.
   </form>

</body> </html>

File: Default.master.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class SiteTemplate : System.Web.UI.MasterPage {

   protected void Page_Load(object sender, EventArgs e)
   {
   }

} File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Content Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

   
     Here"s some new content!

</asp:Content></source>


Table layout master page

   <source lang="csharp">

File: Default.master <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="TableMaster" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">

My Header

           <asp:treeview ID="Treeview1" runat="server" Width="150px">
               <Nodes>
                   <asp:TreeNode Text="Root" Value="New Node">
                       <asp:TreeNode Text="Page 1" Value="Page 1"></asp:TreeNode>
                       <asp:TreeNode Text="Page 2" Value="Page 2"></asp:TreeNode>
                   </asp:TreeNode>
               </Nodes>
           </asp:treeview>
           <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
           </asp:contentplaceholder>
           My Footer
   </form>

</body> </html>

File: Default.master.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class TableMaster : System.Web.UI.MasterPage {

   protected void Page_Load(object sender, EventArgs e)
   {
   }
   public bool ShowNavigationControls
 {
   set { Treeview1.Visible = value; }
   get { return Treeview1.Visible; }
 }

}

File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TableContentPage_aspx" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

   Your content goes in this cell.

<asp:Button ID="cmdShow" runat="server" Text="Show" OnClick="cmdShow_Click" /> <asp:Button ID="cmdHide" runat="server" Text="Hide" OnClick="cmdHide_Click" />

</asp:Content> File: Default.aspx.cs

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class TableContentPage_aspx : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
   }
 protected void cmdHide_Click(object sender, EventArgs e)
 {
   TableMaster master = (TableMaster)this.Master;
       master.ShowNavigationControls = false;
 }
 protected void cmdShow_Click(object sender, EventArgs e)
 {
   TableMaster master = (TableMaster)this.Master;
       master.ShowNavigationControls = true;
 }

}</source>


Using the Page Header Property

   <source lang="csharp">

To change the Title or Cascading Style Sheet rules in a Master Page, you can use the Page.Header property.

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       html
       {
           background-color:silver;
           font:14px Arial,Sans-Serif;
       }
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           border:Solid 1px black;
       }
       .leftColumn
       {
           float:left;
           padding:5px;
           width:200px;
           border-right:Solid 1px black;
           height:700px;
       }
       .rightColumn
       {
           float:left;
           padding:5px;
       }
       .clear
       {
           clear:both;
       }
   </style>
   <title>Simple Master</title>

</head> <body>

   <form id="form1" runat="server">
           <asp:contentplaceholder
               id="ContentPlaceHolder1"
               runat="server"/>
           <asp:contentplaceholder
               id="ContentPlaceHolder2"
               runat="server"/>
       
   </form>

</body> </html> File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master" %> <script runat="server">

   void Page_Load()
   {
       Page.Header.Title = String.Format("Header Content ({0})", DateTime.Now);
       Style myStyle = new Style();
       myStyle.BackColor = System.Drawing.Color.Red;
       Page.Header.StyleSheet.CreateStyleRule(myStyle, null, "html");
   }

</script> <asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content in the first column
   
Content in the first column
Content in the first column
Content in the first column
Content in the first column

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Content in the second column
   
Content in the second column
Content in the second column
Content in the second column
Content in the second column

</asp:Content></source>


Using the Title Attribute

   <source lang="csharp">

There is one requirement for the Title attribute to work. <head> tag in the Master Page must be a server-side Head tag. <head> tag must include the runat="server" attribute.

File: Default.master <%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <style type="text/css">
       html
       {
           background-color:silver;
           font:14px Arial,Sans-Serif;
       }
       .content
       {
           margin:auto;
           width:700px;
           background-color:white;
           border:Solid 1px black;
       }
       .leftColumn
       {
           float:left;
           padding:5px;
           width:200px;
           border-right:Solid 1px black;
           height:700px;
       }
       .rightColumn
       {
           float:left;
           padding:5px;
       }
       .clear
       {
           clear:both;
       }
   </style>
   <title>Simple Master</title>

</head> <body>

   <form id="form1" runat="server">
           <asp:contentplaceholder
               id="ContentPlaceHolder1"
               runat="server"/>
           <asp:contentplaceholder
               id="ContentPlaceHolder2"
               runat="server"/>
       
   </form>

</body> </html> File: Default.aspx <%@ Page Language="C#" MasterPageFile="~/Default.master"

 Title="Content Page Title" %>

<asp:Content

   ID="Content1"
   ContentPlaceHolderID="ContentPlaceHolder1"
   Runat="Server">
   Content in the first column
   
Content in the first column
Content in the first column
Content in the first column
Content in the first column

</asp:Content> <asp:Content

   ID="Content2"
   ContentPlaceHolderID="ContentPlaceHolder2"
   Runat="Server">
   Content in the second column
   
Content in the second column
Content in the second column
Content in the second column
Content in the second column

</asp:Content></source>


What is a Master page

   <source lang="csharp">

Master pages are similar to ordinary ASP.NET pages. Master pages are text files that can contain HTML, web controls, and code. Master pages have a different file extension .master instead of .aspx. Master pages must be used by other pages, which are known as content pages. Master page defines the page structure and the common ingredients. The content pages adopt this structure and just fill it with content. You can use a Master Page to create a common page layout. You can use Master Pages to display common content in multiple pages. You can add multiple Master Pages to the same application. Master page file contains a <%@ Master %> directive instead of the normal <%@ Page %> directive. Content from the content page appears in the ContentPlaceHolder controls during merging. You cannot cache a Master Page with the OutputCache directive. You cannot apply a theme to a Master Page.</source>