Solved

set masterpage file dynamically- c#

Posted on 2014-07-26
4
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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 34

Author Closing Comment

by:Mike Eghtebas
ID: 40238291
Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain purpose of GZIP 4 81
Put File 6 34
Timeouts during development 3 27
Pdf not loading 2 38
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

737 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