Section 8.3. Store Themes in User Profiles

8.3. Store Themes in User Profiles

Note: Allowing users to select a theme is one thing, but you need to be able to remember their preferences so that when they return, their choice remains the one they selected.

In the previous lab, Section 8.2, you saw how to make it possible for your users to change the theme of a page dynamically. The technique is fine as far as it goes, but the next time the user visits your site, she will have to select the theme again. Instead of asking a user to select a theme every time she visits your site, it is better for you to store the user's preferred theme in the Profile object.

8.3.1. How do I do that?

Note: The Profile object is used to personalize a site or a user. Use the Profile object to remember a user's preferences.

In this lab, you will use the Profile object to store the theme selected by the user. When the user visits the page again, the theme is then retrieved from the Profile object and reapplied to the page.

  1. Using the project created in the previous lab, add a Web.config file to the project.

  2. Add the <profile> element to the Web.config file and add a Theme profile property. The Theme property will be used to save the theme selected by a user:

    <system.web>     <profile>         <properties>             <add name="Theme" type="System.String" />         </properties>     </profile> ...

  3. If you want to allow anonymous users to save their profiles, remember to add the <anonymousIdentification> element and the allowAnonymous attribute for each profile property.

  4. To save the theme into the Profile object, switch to the code-behind of the default form and code the Set button. The theme selected in the drop-down listbox is saved into the theme property:

    Protected Sub btnSet_Click(ByVal sender As Object, _                            ByVal e As System.EventArgs) _                            Handles btnSet.Click     Profile.Theme = ddlThemes.SelectedValue End Sub

  5. In the Page_PreInit event, first check to see if there is any item selected in the drop-down list control. If it is empty, proceed to retrieve the profile saved in the Profile property. Otherwise, load the theme set in the drop-down list control:

    Protected Sub Page_PreInit(ByVal sender As Object, _                            ByVal e As System.EventArgs) _                            Handles Me.PreInit     Dim Theme As String = Request("ddlThemes")     '---theme is not set in the DropDownList...     If Theme = "" Then         '---get it from profile         Page.Theme = Profile.Theme     Else         '---get it from DropDownList         Page.Theme = Theme     End If End Sub

  6. Press F5 to test the application. Select the Colorful theme. Stop debugging in Visual Studio 2005 and then press F5 again. You will notice that the Colorful theme is selected. This proves that your page can now remember the user's selected theme the next time she visits.

8.3.2. What about...

...applying themes at the application level?

You can apply a theme to an entire application by adding the <pages> element in Web.config:

<configuration xmlns="">    <system.web>       <pages theme="Colorful" />    </system.web> </configuration>

In the previous example, pages that do not have the Theme attribute in the Page directive will use the Colorful theme. If a page has the Theme attribute set, it will override the settings in Web.config.

8.3.3. Where can I learn more?

To learn more about the Profile service, refer to Chapter 7, where I discuss in more detail how to use the Profile service for authenticated and anonymous users.

ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
Year: 2005
Pages: 104 © 2008-2017.
If you may any questions please contact us: