Solved

set masterpage file dynamically- c#

Posted on 2014-07-26
4
199 Views
Last Modified: 2014-08-03
The only way it works is when I change it manually to:

<%@ Page Title="" Language="C#"  AutoEventWireup="false"    MasterPageFile="~/MasterPage.master" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

or 

<%@ Page Title="" Language="C#"  AutoEventWireup="false"    MasterPageFile="~/MasterPage2.master" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

Open in new window

To do this dynamically, from the a control on the master page, I open a page using:

Response.Redirect("Default2.aspx", false);    //<-- have tried true also

and in the Default2.aspx.cs I have:
public partial class Default2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       
    }
    protected void Page_Preint(object sender, EventArgs e)
    {
        MasterPage myMaster=Master as MasterPage;
      Label lbl = (Label)myMaster.FindControl("lblCmdOpt");
        if (lbl.Text == "f123")
        {
        MasterPageFile =  "~//MasterPage.master";
       }
        else
       {
            MasterPageFile = "~//MasterPage2.master";
        }
    }
}

Open in new window


But the problem is Page_Preint doesn't fire.

Question: How can I change the masterPage dynamically?

Thank you.
0
Comment
Question by:Mike Eghtebas
  • 2
4 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 150 total points
ID: 40222475
Could be the issue is simply that you're not firing the event. So, make sure in the page directive you set AutoEventWireup = false rather than true.  Second, in the page code behind, add the override for the OnInit:

    protected override void OnInit(EventArgs e)
    {
        PreInit += MyApp_PreInit;
        base.OnInit(e);
    }

    void MyApp_PreInit(object sender, EventArgs e)
    {
               MasterPage myMaster=Master as MasterPage;
      Label lbl = (Label)myMaster.FindControl("lblCmdOpt");
        if (lbl.Text == "f123")
        {
        MasterPageFile =  "~//MasterPage.master";
       }
        else
       {
            MasterPageFile = "~//MasterPage2.master";
        }
    }

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40222808
Hi Dan,

I have AutoEventWireup="false"  and also have kept MasterPageFile="~/MasterPage.master"  because removing it doesn't compile:
<%@ Page Title="" Language="C#"  AutoEventWireup="false"    MasterPageFile="~/MasterPage.master" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

Open in new window


I added breaks at line 3 and 9 to see if the code is firing.

Line 3 is firing when the application first lunched.

But although Response.Redirect("Default2.aspx",false); fires, line 9 never does.

Question: What else I should do for the line 9 fires?

Thank you,

Mike
0
 
LVL 7

Accepted Solution

by:
Raghu Mutalikdesai earned 350 total points
ID: 40223653
There is an excellent tutorial from Microsoft on this topic. It is fairly elaborate and step by step guide with sample usage. You need to explore <%@ MasterType VirtualPath="" %> in effect.

Here is the link:
http://www.asp.net/web-forms/tutorials/master-pages/specifying-the-master-page-programmatically-cs

MSDN also has an introductory help:
http://msdn.microsoft.com/en-us/library/vstudio/c8y19k6h(v=vs.100).aspx

It is recommended that you use inheritance for your master pages (a hierarchy of master pages).
If you intend to select a master page dynamically, it is recommended that you create a base class from which your master pages derive
0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 40238291
Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
Cant save 3D 4 20
Can I command line build a C# application? 6 33
VS2013 publish C# application Created folder -- where is it??? 10 16
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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