Cyber Security & Dot Net Security

Monday, September 27, 2010

Dynamically change master page

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">
    <h2>
        This content page is currently bound to <span style="color:Blue">
        <% =this.MasterPageFile %></span>.
    </h2>
</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">
    <div>
        <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>        
    </div>
    </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">
    <div>
        <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>        
    </div>
    </form>
</body>
</html>

No comments: