March 2008
Intermediate to advanced
766 pages
21h 15m
English
You would implement the favorite theme as a String property and call it FavoriteTheme. To ensure that you always have a valid theme, you could also set a default value. Finally, you should make the property accessible to anonymous users. Your final Profile property could end up like this:
<add name="FavoriteTheme" defaultValue="Monochrome" allowAnonymous="true" />
Given the syntax you saw in the question, you could now access the new property and use it to change the current theme in the BasePage:
VB.NET
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.PreInit
Dim myProfile As ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)
If Not String.IsNullOrEmpty(myProfile.FavoriteTheme) Then
Page.Theme = myProfile.FavoriteTheme
End If
End Sub
C#
private void BasePage_PreInit(object sender, EventArgs e)
{
ProfileCommon myProfile = (ProfileCommon) HttpContext.Current.Profile;
if (!string.IsNullOrEmpty(myProfile.FavoriteTheme))
{
Page.Theme = myProfile.FavoriteTheme;
}
}
To finalize the theme selector using Profile, you also need to change the code in the master page. Instead of storing the user-selected theme in a cookie, you should now store it in Profile instead. Change the code in Page_Load as follows:
VB.NET
If Not Page.IsPostBack Then Dim selectedTheme As String = Page.Theme If Not String.IsNullOrEmpty(Profile.FavoriteTheme) ...