troubleshooting Question

dynamically change master page, c#

Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America asked on
C#.NET ProgrammingASP.NET
4 Comments1 Solution1366 ViewsLast Modified:
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");
    }
}
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>
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>
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>
ASKER CERTIFIED SOLUTION
Miguel Oz
Senior Software Engineer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros