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; }
}
}
Related examples in the same category