Solved

set masterpage file dynamically- c#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
fomat Json objects 6 28
Definitions and default visual studio colors 5 58
DateTimepicker 4 33
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

21 Experts available now in Live!

Get 1:1 Help Now