Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

set masterpage file dynamically- c#

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.
SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.