dynamically change master page, c#

I have a website with 4 pages:
1. MasterPage.master
2. MasterPage2.master
3. f456.aspx           <-- uses MasterPage2.master
4. Default2.aspx   <--  uses MasterPage.master, but because Default2.aspx is identical to f456.aspx, it should be eliminated.

Question: How can I call f456.aspx and dynamically change its master page as shown below:

                            /----- use MasterPage2.master   when Session["MasterPageName"]="MasterPage2.master";
f456.aspx
                           \----- use MasterPage.master  when Session["MasterPageName"]="MasterPage.master";

I need the details of changes in both f456.aspx and f456.aspx.cs files.

Thank you.

1. MasterPage.master:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>ROD</title>
    <link href="style/masterPage.css" rel="stylesheet" type="text/css" />
    <link href="style/form1through6.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <style type="text/css">
        #masterTable
        {
            color: White;
            font-family: Tahoma;
        }
        #Button4
        {
            background-color: yellow;
        }
        #Button3
        {
            background-color: lightblue;
        }
        #Button5
        {
            background-color: green;
        }
        #Button6
        {
            background-color: orange;
        }
    </style>
</head>
<body class="body">
    <form id="form1" runat="server">
    <table border="1" id="masterTable">
        <tr id="firstRow">
            <td class="style1">
                Content area for data from f456.aspx (or Default2.aspx)
                <br />
                The goal is to have only one page for both (use f456.aspx, delete Default2.aspx)
                <br />
                MasterPage.maste <-- MasterPage2.maste <-- f456.aspx<br />
                MasterPage.maste <-- f456.aspx (replaces Default2.aspx)<br />
                Meaning the master page for f456.aspx has to change dynamically(this is where I
                need help).
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </td>
            <td class="style1">
                Content area for data from MasterPage2.master (when f456.aspx button is clicked
                on)
                <asp:ContentPlaceHolder ID="ContentPlaceHolderSide" runat="server">
                </asp:ContentPlaceHolder>
            </td>
        </tr>
        <tr>
            <td id="secondRow" class="style2">
                Some Controls (Exist in MasterPage.master)
            </td>
            <td class="style1">
                Empty Cell 1 (not used)
            </td>
        </tr>
        <tr>
            <td>
                Button set 1 (Exists in MasterPage.master)<br />
                <asp:Button ID="Button2" runat="server" Text="Home" OnClick="Button2_Click" />
                <asp:Button ID="Button3" runat="server" Text="f456.aspx" OnClick="Button3_Click" /><br />
                <asp:Button ID="Button4" runat="server" Text="Default2.aspx" OnClick="Button4_Click" /><br />
                <asp:Button ID="Button5" runat="server" Text="Multi Chart" OnClick="Button5_Click" />
                <asp:Button ID="Button6" runat="server" Text="Single Chart" OnClick="Button6_Click" />
            </td>
            <td>
                Empty Cell 2 (not used)
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

*******************MasterPage.master.cs******************************
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/f456.aspx");
 
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default2.aspx");
  
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/monthlyMultiChart9.aspx");
       
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/monthlySingleChart9.aspx");
    }
}

Open in new window

2. MasterPage2.master:
<%@ Master Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <style type="text/CSS">
#notes
{
    color:lightblue;
    
}
</style>
    <asp:ContentPlaceHolder ID="head" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="Server">
    </asp:ContentPlaceHolder>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderSide" runat="Server">
      <p><span id="notes">Side Gridview Data from MasterPage2.master.aspx (when f456.aspx button is clicked on)</span></p>
</asp:Content>

Open in new window

3. f456.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="f456.aspx.cs" Inherits="f456" %>
<%@ MasterType  VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/CSS">#notes{    color:lightblue;}</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p><span id="notes">Two Gridview Data from f456.aspx</span></p>
</asp:Content>

Open in new window

4. Default2.aspx:
<%@ Page Title="" Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ MasterType  VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <style type="text/CSS">#notes{ color:yellow;}</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <p><span id="notes">Two Gridview Data from Default2.aspx at this point. Default2.aspx to be replaced by f456.aspx later.</span></p>
</asp:Content>

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Implement PreInit of your page as follows: (assuming MasterPage is in your root folder)
protected void Page_PreInit(object sender, EventArgs e)
 {
 this.Master.MasterPageFile = "/MasterPage.Master";
 }

Open in new window


For more details check Attaching Master Pages Dynamically section
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Do I need some adjustments at the directive of f456.aspx?

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="f456.aspx.cs" Inherits="f456" %>
<%@ MasterType  VirtualPath="~/MasterPage.master" %>
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I got the following exception:

Unable to cast object of type 'ASP.masterpage2_master' to type 'ASP.masterpage_master'.

But, after I revised it to:

this.MasterPageFile = "~/MasterPage.Master";

it is working okay.

Thanks,

Mike
0
Miguel OzSoftware EngineerCommented:
As per MSDN example in my previous post: If you are using MasterType  directive you must create a base master that holds of your common properties. This fix will eliminate the error as well.

You should check whether your code requires this directive. (e.g. You are accessing Master page properties) otherwise remove it from  you child page.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.