Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Dave Sussman's .NET Blog

writerus drivelus

ASP.NET 2.0 Themes and Profiles

Here's something that might be useful if you're starting to look at ASP.NET 2.0. Master pages allow you to centralise the site design, Themes allow users to customise (to a degree) UI and the personalization Profile allows easy configuration and access of personal settings (such as the theme). Because of the way the control tree is constructed, the Theme either has to be set declaratively (in the Page directive or web.config) or in the new PreInit event. The question is what do you do if you want all pages to a) use a master and b) have the Theme set from the Profile. Common sense would dictate that you use the PreInit event in the master page, but it doesn't exist, forcing you to use PreInit in every page. Now I don't like typing in the same code into every page, and I suspect you're the same.

The solution is to use a custom base class for the page, which can be set for the entire site in web.config:

<pages masterPageFile="site.master" pageBaseType="MyPage" />

Now create your custom base type, placing the file in the Code directory to avoid all that nasty compilation step:

 Public Class MyPage Inherits Page Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs) Dim p As HttpProfileBase If User.Identity.IsAuthenticated Then p = Profile.HttpProfileBase.Create(User.Identity.Name) Else p = Profile.HttpProfileBase.Create(Request.AnonymousId) End If Page.Theme = p.GetPropertyValue("Theme").ToString() MyBase.OnPreInit() End Sub End Class 

Since all of your pages inherit from this class, they automatically inherit this behaviour, having their Theme set from the Profile property of the same name. It even works for anonymous users (don't forget to mark the property as allowAnonymous).

Sponsor
Published Monday, April 26, 2004 9:05 PM by dsussman
Filed under:

Comments

 

dsussman said:

Thanks for that Dave... a nice tip!
April 26, 2004 6:38 PM
 

dsussman said:

Yeah, it kinda sucks that there's no Pr_Init on the master. While a good solution it's not really elegant enough for me. Hmm, and I really must learn how to post code properly - all on one line ain't great.
April 26, 2004 6:58 PM
Anonymous comments are disabled