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

Asked

Viewed 37 times

1

I wanted something like this (it doesn’t work). "The Masterpagefile property can only be set in 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

1 answer

2


As the error says, to tell which Masterpage to use, use the event Page_PreInit

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

You can read more about it on Documentation

  • worked, I was long back from this , my brain gave tilt nor I paid attention to the error Thank you !

  • @Pedrocardoso I’m glad to have helped, don’t forget to mark as accepted ;) so if others have the same question they will see how you solved the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.