ASP.Net/User Control and Master Page/Master page

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

Содержание

A content page that can work with more than one master page

 
<%@ Page Language="VB" MasterPageFile="~/Default.master"
       Mozilla:MasterPageFile="~/MyMozilla.master"
       Opera:MasterPageFile="~/MyOpera.master" %>
    
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1"
  Runat="server">
    Hello World
</asp:Content>



A master page that creates a GUID on the first request (C#)

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Untitled Page" %>
<script runat="server">
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        Label1.Text = (Master.FindControl("Label1") as Label).Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label2.Text = "<b>Hello " + TextBox1.Text + "!</b>";
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="server">
    <b>Your GUID number from the master page is:<br />
    <asp:Label ID="Label1" Runat="server" /></b><p>
    <b>Enter your name:</b><br />
    <asp:Textbox ID="TextBox1" Runat="server" />
    <asp:Button ID="Button1" Runat="server" Text="Submit" 
     OnClick="Button1_Click" /><br />
    <br />
    <asp:Label ID="Label2" Runat="server" />
</asp:content>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2" Runat="server">
        <asp:Image ID="Image1" Runat="server" ImageUrl="http://www.nfex.ru/style/logo.png" />
</asp:Content>
File: Default.master
<%@ Master Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label1.Text = System.Guid.NewGuid().ToString();
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Company Home Page</h1>
                    <b>User"s GUID:&nbsp;&nbsp;
                        <asp:Label ID="Label1" Runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2006 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



A master page that creates a GUID on the first request (VB)

<%@ Page Language="VB" MasterPageFile="~/Default.master" %>
<script runat="server" language="vb">
    Protected Sub Page_LoadComplete(ByVal sender As Object, _
       ByVal e As System.EventArgs)
        Label1.Text = CType(Master.FindControl("Label1"), Label).Text
    End Sub
  
    Protected Sub Button1_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs)
        Label2.Text = "<b>Hello " & TextBox1.Text & "!</b>"
    End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="server">
    <b>Your GUID number from the master page is:<br />
    <asp:Label ID="Label1" Runat="server" /></b>
    <b>Enter your name:</b><br />
    <asp:Textbox ID="TextBox1" Runat="server" />
    <asp:Button ID="Button1" Runat="server" Text="Submit" 
     OnClick="Button1_Click" /><br />
    <asp:Label ID="Label2" Runat="server" />
</asp:content>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2" Runat="server">
        <asp:Image ID="Image1" Runat="server" ImageUrl="http://www.nfex.ru/style/logo.png" />
</asp:Content>

File: Default.master
<%@ Master Language="VB" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)     
        If Not Page.IsPostBack Then
            Label1.Text = System.Guid.NewGuid().ToString()
        End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <b>User"s GUID:
                    <asp:Label ID="Label1" Runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server"/>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2006 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



A master page that exposes a custom property (C#)

<%@ Page Language="C#" MasterPageFile="~/Default.master" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<script runat="server">    
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.PageHeadingTitle = "My Company ?Division X";
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"/>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"/>
File: Default.master
<%@ Master Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label1.Text = System.Guid.NewGuid().ToString();
        }
    }
    string m_PageHeadingTitle = "My Company";
    public string PageHeadingTitle
    {
        get
        {
            return m_PageHeadingTitle;
        }
        set
        {
            m_PageHeadingTitle = value;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="Form1" runat="server">
        <table>
            <tr>
                <td>
                    <h1><%= PageHeadingTitle%></h1>
                    <b>User"s GUID:
                        <asp:Label ID="Label1" Runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server"/>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2004 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



A master page that exposes a custom property (VB)

<%@ Page Language="VB" MasterPageFile="~/Default.master" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<script runat="server" language="vb">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Master.PageHeadingTitle = "My Company"
    End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"/>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"/>
File: Default.master
<%@ Master Language="VB" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)     
        If Not Page.IsPostBack Then
            Label1.Text = Guid.NewGuid().ToString()
        End If
    End Sub
    
    Dim m_PageHeadingTitle As String = "My Company"
    
    Public Property PageHeadingTitle() As String
        Get
            Return m_PageHeadingTitle
        End Get
        Set(ByVal Value As String)
            m_PageHeadingTitle = Value
        End Set
    End Property
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="Form1" runat="server">
        <table>
            <tr>
                <td>
                    <h1><%= PageHeadingTitle %></h1>
                    <b>User"s GUID:&nbsp;&nbsp;
                        <asp:Label ID="Label1" Runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2004 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



Code behind and master page

<%@ Page Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" 
 Runat="server">
    <b>Enter your name:</b><br />
    <asp:Textbox ID="TextBox1" Runat="server" />
    <asp:Button ID="Button1" Runat="server" Text="Submit" 
     OnClick="Button1_Click" /><br />
    <br />
    <asp:Label ID="Label1" Runat="server" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2" Runat="server">
        <asp:Image ID="Image1" Runat="server" ImageUrl="http://www.nfex.ru/style/logo.png" />
</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 Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "<b>Hello " + TextBox1.Text + "!</b>";
    }
}

File: Default.master
<%@ Master Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Company Home Page</h1>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server"/>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2006 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



Control title in master page with content page code

<%@ page language="C#" MasterPageFile="~/Default.master" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<script runat="server" language="c#">
void Page_Load(object sender, System.EventArgs e) {
        this.Master.HtmlTitle = "Hello World!";
    }
</script>
File: Default.master
<%@ master language="C#" %>
<script runat="server">
    public string HtmlTitle {    
        get { return this.literal1.Text; }    
        set { this.literal1.Text = value; }
    }
</script>
<html>
<head runat="server">
    <title><ASP:Literal id="literal1" runat="server" /></title>
</head>
<body>
    <form runat="server">
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"/>
    </form>
</body>
</html>



Exposing a server control from a master page as a public property (C#)

<%@ Page Language="C#" MasterPageFile="~/Default.master" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<script runat="server" language="C#">    
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.MasterPageLabel.Font.Size = 25;
    }
</script>
File: Default.master

<%@ Master Language="VB" %>
<script runat="server" language="C#">    
    public Label MasterPageLabel
    {
        get
        {
            return Label1;
        }
        set
        {
            Label1 = value; 
        }
    } 
</script>
 <asp:Label ID="Label1" Runat="server" />



Exposing a server control from a master page as a public property (VB)

<%@ Page Language="VB" MasterPageFile="~/Default.master" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<script runat="server" language="vb">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Master.MasterPageLabel1.Font.Size = 25
    End Sub
</script>

File: Default.master
<%@ Master Language="VB" %>
<script runat="server" language="vb">    
    Public Property MasterPageLabel1() As Label
        Get
            Return Label1
        End Get
        Set(ByVal Value As Label)
            Label1 = Value
        End Set
    End Property
</script>
 <asp:Label ID="Label1" Runat="server" />



Link content page with master page

File: Default.master
<%@ Master Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC 
   "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Acme Pirate Store</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img src="http://www.nfex.ru/style/logo.png" /><br />
        <asp:contentplaceholder id="ContentPlaceholder1"
            runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

File: index.aspx

<%@ Page Language="C#" 
    MasterPageFile="~/Default.master" 
    AutoEventWireup="true" 
    Title="Product Page" %>
<asp:Content ID="Content1" 
    ContentPlaceHolderID="ContentPlaceholder1" 
    Runat="Server">
    <br />
    Product:
    <asp:DropDownList ID="ddlItem" runat="server" 
        Width="140px">
        <asp:ListItem Text="P"></asp:ListItem>
        <asp:ListItem>A</asp:ListItem>
        <asp:ListItem>B</asp:ListItem>
        <asp:ListItem>C</asp:ListItem>
    </asp:DropDownList><br />
    <br />
    Quantity:
    <asp:TextBox ID="txtQuantity" runat="server" 
        Width="82px" />
    <br /><br />
</asp:Content>



master for print

File: index.aspx
<%@ page language="C#" MasterPageFile="~/main.master" printcontent:master="~/print.master" %>
<script runat="server" language="c#">
  public override bool TestDeviceFilter(string deviceFilterName) {
    if (deviceFilterName.Equals("printcontent")) {
        if (this.Request.QueryString["print"] != null) {
            return true;
        }
    }
    return base.TestDeviceFilter(deviceFilterName);
  }
</script>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
    Test Test<br />
    <br />
    <asp:hyperlink id="HyperLink1" runat="server" navigateurl="Default.aspx?print=1">Print
        me!</asp:hyperlink>
</asp:content>

File: main.master
<%@ master language="C#" %>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"/>
    </form>
</body>
</html>

File: print.master
<%@ master language="C#" %>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        This master can be used for printing.<br />
        <br />
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"/>
    </form>
</body>
</html>



Master in page derivative

<%@ page language="C#" MasterPageFile="~/Default.master" %>
<asp:content id="Content1" 
             contentplaceholderid="ContentPlaceHolder1" 
             runat="server">
             this is a test
</asp:content>

File: Default.master

<%@ master language="C#" %>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Little Company</h1>
                </td>
            </tr>
            <tr>
                <td>Navigation?</td>
                <td>
                    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                    </asp:contentplaceholder>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



Master pages exposing an object model for content pages to programmatically modify elements on the master.

<%@ Page Language="C#" MasterPageFile="Default.master" 
    AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default" 
    Title="Hello, Master" 
    %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentOfThePage" runat="server">
    <h1>Welcome to this page!</h1>
    <h3>master!</h3>
</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.Drawing;
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((ASP.MasterWithProp)Master).TitleBoxText = "ASP.NET";
        ((ASP.MasterWithProp)Master).TitleBoxForeColor = Color.Cyan;
    }
}
File: Default.master
<%@ Master  Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" 
    Inherits="MasterPages_SimpleWithProp" Classname="MasterWithProp" %>
<!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>
    <div id="pageContent">
        <form id="form1" runat="server">
            <asp:Panel ID="HeaderPanel" runat="server" Height="50px" Width="100%">
                <asp:Label ID="TitleBox" 
                           runat="server" 
                           EnableTheming="False" 
                           Font-Bold="True" 
                           Font-Names="Impact"
                           Text="Anything" 
                           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 ID="SubTitleBox" 
                           runat="server" 
                           EnableTheming="False" 
                           Font-Bold="True" 
                           Font-Names="Lucida Console"
                           Text="sub title" 
                           Font-Size="Large" 
                           ForeColor="Orange"></asp:Label>
            </asp:Panel>        
        </form>
    </div>
</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.Drawing;
public partial class MasterPages_SimpleWithProp : System.Web.UI.MasterPage
{
  public string TitleBoxText
  {
    get { return TitleBox.Text; }
    set { TitleBox.Text = value; }
  }
    public Color TitleBoxForeColor
    {
        get { return TitleBox.ForeColor; }
        set { TitleBox.ForeColor = value; }
    }
}



Master page with Default Content

Default content will appear unless it is overridden in a particular content 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>Default Master</title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="content">
        <div class="leftColumn">
            <asp:contentplaceholder
                id="ContentPlaceHolder1"
                runat="server"/>
        </div>
        <div class="middleColumn">
            <asp:ContentPlaceholder
                id="ContentPlaceHolder2"
                runat="server" />
        </div>
        <div class="rightColumn">
            <asp:ContentPlaceHolder
                id="contentAd"
                Runat="server">
                <asp:Image
                    id="imgAd"
                    ImageUrl="~/BannerAd.gif"
                    CssClass="ad"
                    AlternateText="Advertisement"
                    Runat="server" />
            </asp:ContentPlaceHolder>
        </div>
        <br class="clear" />
    </div>
    </form>
</body>
</html>
File: DefaultContent.aspx
<%@ Page Language="C#" MasterPageFile="~/Default.master" %>
<asp:Content
    ID="Content1"
    ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    Content in the first column
    <br />Content in the first column
    <br />Content in the first column
    <br />Content in the first column
    <br />Content in the first column
</asp:Content>
<asp:Content
    ID="Content2"
    ContentPlaceHolderID="ContentPlaceHolder2"
    Runat="Server">
    Content in the second column
    <br />Content in the second column
    <br />Content in the second column
    <br />Content in the second column
    <br />Content in the second column
</asp:Content>



Master page with trace (C#)

<%@ page language="C#" MasterPageFile="MasterPage2.master" Trace="true" %>
<script runat="server" language="c#">
</script>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    Here"s some text!
</asp:content>
<%--
<%@ master language="C#" %>
<script runat="server">
</script>
<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="Form1" runat="server">
        <table id="Table1" cellspacing="1" cellpadding="1" width="100%" border="1">
            <tr>
                <td colspan="2">
                    <h1>My Little Company</h1>
                </td>
            </tr>
            <tr>
                <td>Navigation?</td>
                <td>
                    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                    </asp:contentplaceholder>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td><asp:contentplaceholder id="Contentplaceholder2" runat="server">This is a default content
                        that can be replaced in any content page</asp:contentplaceholder>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

--%>



Nested master

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Sample Nested Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" Runat="Server">
  <h1>Book Rep System Home</h1>
  <p>Welcome to the book rep system</p>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="sideContent" Runat="Server">
  <h2>New Releases</h2>
  <p>Core C#</p>
</asp:Content>
File: Default.master
<%@ Master Language="C#" MasterPageFile="~/MasterDefault.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="parentContent" Runat="Server">
   <div id="container">
      <div id="header">
      <img src="~/logo.gif" />
      <asp:Image runat="server" ID="imgLogo" ImageUrl="~/cool.gif" />
      </div>
      <div id="sideArea">
         <div id="menu">
            <asp:BulletedList ID="blstSample" runat="server" DisplayMode="HyperLink">
               <asp:ListItem Value="">Home</asp:ListItem>
               <asp:ListItem Value="">Products</asp:ListItem> 
               <asp:ListItem Value="">About</asp:ListItem>
            </asp:BulletedList>
         </div>
         <div id="sideAreaBox">
            <asp:ContentPlaceHolder ID="sideContent" runat="server">
              <p>default side content</p>
            </asp:ContentPlaceHolder>  
         </div>          
      </div>
      <div id="mainArea">
         <asp:ContentPlaceHolder ID="mainContent" runat="server">
           <p>default main content</p>
         </asp:ContentPlaceHolder>
      </div>
      <div id="footer">
         <p>footer</p>
      </div>         
   </div>
</asp:Content>
File: MasterDefault.master
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="topNav">
       <p>
       Pearson Ed Navigator<br />
       <asp:DropDownList ID="drpPlaces" runat="server">
          <asp:ListItem>Book System</asp:ListItem>
         <asp:ListItem>A</asp:ListItem>
         <asp:ListItem>B</asp:ListItem>
       </asp:DropDownList>
       </p>
    </div>
    <asp:contentplaceholder id="parentContent" runat="server"/>
    </form>
</body>
</html>



Nested master page

<%@ page language="C#" MasterPageFile="~/Default.master" %>
<asp:content id="Content1" contentplaceholderid="Content1" runat=server>
   <asp:label runat="server" id="Label1" 
        text="Child label1" font-bold="true"/>
   <br>
</asp:content>
<asp:content id="Content2" contentplaceholderid="Content2" runat=server>
   <asp:label runat="server" id="Label2" 
        text="Child label2" font-bold="true"/>
</asp:content>
File: Default.master
<%@ master language="C#" MasterPageFile="~/MasterDefault.master" %> 
<asp:content id="Content1" contentplaceholderid="MainContent" runat="server">
   <asp:panel runat="server" id="panelMain" backcolor="lightyellow">
   <h2>Child master</h2>
      <asp:panel runat="server" id="panel1" backcolor="lightblue">
        <p>This is childmaster content.</p>
        <asp:contentplaceholder id="Content1" runat="server" />
      </asp:panel>
      <asp:panel runat="server" id="panel2" backcolor="pink">
         <p>This is childmaster content.</p>
         <asp:contentplaceholder id="Content2" runat="server" />
      </asp:panel>
   </asp:panel>
</asp:content>

File: MasterDefault.master
<%@ Master Language="C#" %> 
<HTML>
<BODY>
<form id="Form1" runat=server>
   <h1>Parent Master</h1>
   <p>
   <font color=red>This is Parent-master content.</font>
   </P>
<asp:contentplaceholder id="MainContent" runat=server/>
</form>
</HTML>
</BODY>



Nested master page (C#)

<%@ page language="C#" MasterPageFile="MasterPage5.master" %>
<asp:content id="Content1" contentplaceholderid="Content1" runat=server>
   <asp:label runat="server" id="Label1" 
        text="Child label1" font-bold=true/>
   <br>
</asp:content>
<asp:content id="Content2" contentplaceholderid="Content2" runat=server>
   <asp:label runat="server" id="Label2" 
        text="Child label2" font-bold=true/>
</asp:content>

<%-- MasterPage5.master
<%@ master language="C#" MasterPageFile="MasterMasterPage5.master" %> 
<asp:content id="Content1" contentplaceholderid="MainContent" runat="server">
   <asp:panel runat="server" id="panelMain" backcolor="lightyellow">
   <h2>Child master</h2>
      <asp:panel runat="server" id="panel1" backcolor="lightblue">
        <p>This is childmaster content.</p>
        <asp:contentplaceholder id="Content1" runat="server" />
      </asp:panel>
      <asp:panel runat="server" id="panel2" backcolor="pink">
         <p>This is childmaster content.</p>
         <asp:contentplaceholder id="Content2" runat="server" />
      </asp:panel>
   </asp:panel>
</asp:content>
--%>

<%-- MasterMasterPage5.master
<%@ Master Language="C#" %> 
<HTML>
<BODY>
<form id="Form1" runat=server>
   <h1>Parent Master</h1>
   <p>
   <font color=red>This is Parent-master content.</font>
   </P>
<asp:contentplaceholder id="MainContent" runat=server/>
</form>
</HTML>
</BODY>
--%>



Overriding some default content in the content page (C#)

<%@ Page Language="C#" MasterPageFile="~/Default.master" %>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2"  
 Runat="server">
    This is new content
</asp:Content>

File: Default.master

<%@ Master Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server">
        Here is some default content
        </asp:ContentPlaceHolder><p>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server">
        Here is some more default content
        </asp:ContentPlaceHolder></p>
    </form>
</body>
</html>



Overriding some default content in the content page (VB)

<%@ Page Language="VB" MasterPageFile="~/Default.master" %>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2"  
 Runat="server">
    This is new content
</asp:Content>

File: Default.master
<%@ Master Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server">
        Here is some default content
        </asp:ContentPlaceHolder><p>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server">
        Here is some more default content
        </asp:ContentPlaceHolder></p>
    </form>
</body>
</html>



Put site navigation in the master page

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Products" %>
<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" Runat="Server">
<h1>Products</h1>
<p>
infrastructure.
</p>
<p>
and much more.
</p>
</asp:Content>
File: Default.master
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
   <form id="form1" runat="server">
      <div id="container">
         <div id="sideArea">
            <div id="menu">
               <asp:BulletedList ID="blstSample" runat="server" DisplayMode="HyperLink">
                  <asp:ListItem Value="BookHome.aspx">Home</asp:ListItem>
                  <asp:ListItem Value="Default.aspx">Products</asp:ListItem> 
                  <asp:ListItem Value="About.aspx">About</asp:ListItem>
               </asp:BulletedList>
            </div>
            <div id="sideAreaBox">
               <asp:ContentPlaceHolder ID="sideContent" runat="server">
                 <p>default side content</p>
               </asp:ContentPlaceHolder>  
            </div>          
         </div>
         <div id="mainArea">
            <asp:ContentPlaceHolder ID="mainContent" runat="server">
              <p>default main content</p>
            </asp:ContentPlaceHolder>
         </div>
         <div id="footer">
            <p>This site is not real.</p>
         </div>         
      </div>
   </form>
</body>
</html>



Specifying master page in Web.config causes every content page to inherit from the master page

 
<configuration>
    <system.web>
        <pages masterPageFile="~/Default.master" />
    </system.web>
</configuration>
 
Once specified in this manner, the content page"s Page directive can then be : 
<%@ Page Language="VB" %>
You can override the application-wide master page specification by 
declaring a different master page within your content page:
<%@ Page Language="VB" MasterPageFile="~/MyOtherCompany.master" %>



Specifying the master page for a specific folder in the Web.config file: Use <location>

<configuration>
    <location path="AdministrationArea">
        <system.web>
            <pages masterPageFile="~/yourMaster.master" />
        </system.web>
    </location>
    
</configuration>



Two Master Pages (C#)

<%@ page language="C#" MasterPageFile="main.master" printcontent:MasterPageFile="print.master" %>
<script runat="server" language="c#">
  public override bool TestDeviceFilter(string deviceFilterName) {
    if (deviceFilterName.Equals("printcontent")) {
        if (this.Request.QueryString["print"] != null) {
            return true;
        }
    }
    return base.TestDeviceFilter(deviceFilterName);
  }
</script>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
    Test Test<br />
    <br />
    <asp:hyperlink id="HyperLink1" runat="server" navigateurl="TwoMasterPages.aspx?print=1">Print
        me!</asp:hyperlink>
</asp:content>
<%-- main.master
<%@ master language="C#" %>
<script runat="server">
</script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </form>
</body>
</html>
--%>
<%-- print.master
<%@ master language="C#" %>
<script runat="server">
</script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        This master can be used for printing.<br />
        <br />
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </form>
</body>
</html>
--%>



Two pieces of contentplaceholder

<%@ page language="C#" 
         MasterPageFile="~/Default.master" 
         Trace="true" %>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    Here"s some text!
</asp:content>
File: Default.master
<%@ master language="C#" %>
<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="Form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Little Company</h1>
                </td>
            </tr>
            <tr>
                <td>Navigation?</td>
                <td>
                    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td><asp:contentplaceholder id="Contentplaceholder2" runat="server">This is a default content.
                    </asp:contentplaceholder>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



Update the page properties in master page

<%@ Page Language="C#" MasterPageFile="~/Default.master" 
                       AutoEventWireup="true" 
                       CodeFile="Default.aspx.cs" 
                       Inherits="ProgrammedHome" 
                       Title = "Programmed Master Page Home" %>
<%@ MasterType VirtualPath="~/Default.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="sideContent" Runat="Server">
  <h2>title</h2>
  <p>AAA</p>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" Runat="Server">
  <h1>title</h1>
  <p>BBB</p>
</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 ProgrammedHome : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      Master.AdImageUrl = "~/Images/ads/ad3.gif";
      Master.AdNavigateUrl = "http://www.nfex.ru";
   }
}
File: Default.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Default.master.cs" Inherits="ProgrammedContentMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
   <form id="form1" runat="server">
      <div id="container">
         <div id="header">
            <asp:Image runat="server" ID="imgLogo" ImageUrl="~/cool.gif" />
            <div id="bannerAd">
            <asp:HyperLink ID="imgbtnAd" runat="server" />
            </div>
         </div>
         <div id="sideArea">
            <div id="menu">
               <asp:BulletedList ID="blstSample" runat="server" DisplayMode="HyperLink">
                  <asp:ListItem Value="">Home</asp:ListItem>
                  <asp:ListItem Value="">Products</asp:ListItem> 
                  <asp:ListItem Value="">About</asp:ListItem>
               </asp:BulletedList>
            </div>
            <div id="sideAreaBox">
               <asp:ContentPlaceHolder ID="sideContent" runat="server">
                 <p>default side content</p>
               </asp:ContentPlaceHolder>  
            </div>          
         </div>
         <div id="mainArea">
            <asp:ContentPlaceHolder ID="mainContent" runat="server">
              <p>default main content</p>
            </asp:ContentPlaceHolder>
         </div>
         <div id="footer">
            <p>this is a test.</p>
         </div>         
      </div>
   </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 ProgrammedContentMaster : System.Web.UI.MasterPage
{
   public string AdImageUrl
   {
      get { return imgbtnAd.ImageUrl; }
      set { imgbtnAd.ImageUrl = value; }
   }
   public string AdNavigateUrl
   {
      get { return imgbtnAd.NavigateUrl; }
      set { imgbtnAd.NavigateUrl = value; }
   }
}



Use image to fill the filler in master page

<%@ Page Language="C#" MasterPageFile="~/Default.master" %>
<script runat="server" language="c#">    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "<b>Hello " + TextBox1.Text + "!</b>";
    }    
</script>
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="server">
    <b>Enter your name:</b><br />
    <asp:Textbox ID="TextBox1" Runat="server" />
    <asp:Button ID="Button1" Runat="server" Text="Submit" 
     OnClick="Button1_Click" /><br />
    <asp:Label ID="Label1" Runat="server" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2" Runat="server">
        <asp:Image ID="Image1" Runat="server" ImageUrl="http://www.nfex.ru/style/logo.png" />
</asp:content>
File: Default.master
<%@ Master Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Company Home Page</h1>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="server"/>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2006 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



Use master page (C#)

<%@ page language="C#" MasterPageFile="MasterPage3.master" %>
<script runat="server" language="c#">
</script>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    cool stuff!
</asp:content>

<%-- MasterPage3.master
<%@ master language="C#" %>
<script runat="server">
</script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <table id="Table1" cellspacing="1" cellpadding="1" width="100%" border="1">
            <tr>
                <td colspan="2">
                    <h1>My Little Company</h1>
                </td>
            </tr>
            <tr>
                <td>Navigation?</td>
                <td>
                    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                    </asp:contentplaceholder>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

--%>



Using Page_PreInit to assign the master page programmatically (C#)

<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.MasterPageFile = "~/Default.master";
    }
</script>



Using Page_PreInit to assign the master page programmatically (VB)

<%@ Page Language="VB" %>
<script runat="server">
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.MasterPageFile = "~/MyMasterPage.master"
    End Sub
</script>