Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can I change a MasterPage at Runtime?
Message
De
28/04/2016 07:34:18
 
 
À
27/04/2016 19:15:08
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01635492
Message ID:
01635542
Vues:
36
>>Hi,
>>
>>I've inherited an ASP.NET application and am trying to implement security the way they have done it which checks the user's rights (type) during login and then redirects the user to different pages based on different master pages. I now need to let two different types of users access the same page, but of course the Master Page won't work for this new user type.
>>
>>Is there a way I can code the master page to be different depending on a session variable like this:
>>
>>
>>        protected void Page_PreInit(object sender, EventArgs e)
>>        {
>>            if (Session["CanViewInvoiceHistory"] == "true")
>>            {
>>                this.Page.Master.MasterPageFile = "~/SiteSuper.master";
>>            }
>>        }
>>
>>The code gives an error when I run the site:
>>
>>Server Error in '/' Application.
>>
>>Content controls have to be top-level controls in a content page or a nested master page that references a master page.
>>
>>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
>>
>>Exception Details: System.Web.HttpException: Content controls have to be top-level controls in a content page or a nested master page that references a master page.
>
>I think you may be going up a level too far. You are setting the master page for Page.Master. In other words, making a nested master page. Try
>
protected void Page_PreInit(object sender, EventArgs e)
>        {
>            if (Session["CanViewInvoiceHistory"] == "true")
>            {
>                this.Page.MasterPageFile = "~/SiteSuper.master"; // Or this.MasterPageFile if your current class is a Page.
>            }
>        }
Thanks Rob! That looks like it has worked.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform