Solved

set masterpage file dynamically- c#

Posted on 2014-07-26
4
191 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 19

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now