We help IT Professionals succeed at work.

dynamically change master page, c#

1,338 Views
Last Modified: 2014-08-22
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

Comment
Watch Question

Senior Software Engineer
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mike EghtebasDatabase and Application Developer

Author

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" %>
Mike EghtebasDatabase and Application Developer

Author

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
Miguel OzSenior Software Engineer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.