Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

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" %>
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
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.