How to choose (depending on login) which master page to run?

1

I wanted something like this (this one does not work). Error "The 'MasterPageFile' property can only be set on or before the 'Page_PreInit' event."

foreach (DataRow item in dt.Rows)
            {
                if (item["tipoLogin"].ToString().Contains("admin"))
                {

                    MasterPageFile = "~/MasterPage.master";
                    Response.Redirect("HomeAdmin.aspx");
                }
                else
                {
                    if (item["tipoLogin"].ToString().Contains("gestor"))
                    {
                        MasterPageFile = "~/MasterGestor.master";
                        Response.Redirect("HomeGestor.aspx");
                    }
                    else
                    {
                        if (item["tipoLogin"].ToString().Contains("responsavel"))
                        {
                            MasterPageFile = "~/MasterPageResponsavel.master";
                            Response.Redirect("HomeResponsavel.aspx");
                        }
                    }
                }

            }

Thank you

    
asked by anonymous 09.08.2018 / 19:08

1 answer

1

As the error says, to tell which MasterPage to use, use the Page_PreInit

protected void Page_PreInit(object sender, EventArgs e) 
{
    //Sua Logica Aqui
    this.MasterPageFile = "~/SuaMasterPage.master"; 
}

You can read more about this at Documentation

    
09.08.2018 / 19:54