Skip to Content
Beginning ASP.NET 3.5: In C# and VB
book

Beginning ASP.NET 3.5: In C# and VB

by Imar Spaanjaars
March 2008
Intermediate to advanced content levelIntermediate to advanced
766 pages
21h 15m
English
Wrox
Content preview from Beginning ASP.NET 3.5: In C# and VB

A.16. Chapter 16

A.16.1.

A.16.1.1.
A.16.1.1.1. Exercise 1 solution

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" />
A.16.1.1.2. Exercise 2 solution

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;
  }
}
A.16.1.1.3. Exercise 3 solution

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET 4: in C# and VB

Beginning ASP.NET 4: in C# and VB

Imar Spaanjaars
Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Bill Evjen, Scott Hanselman, Devin Rader

Publisher Resources

ISBN: 9780470187593Purchase book