?
Solved

dynamically change master page, c#

Posted on 2014-08-22
4
Medium Priority
?
986 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

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 40279874
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40279895
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40279928
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
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40280022
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question