Using Themes and Skins for Personalization with Visual Basic 2005 - Enable Themes and Skins (Page 3 of 7 )
To let your users choose the theme they like and have their preference stored in their profile, you need to add a single line to the properties element in the profile element of Web.config:
<add name="Theme" />
Save and rebuild your application.
Specify Themes for Your Page
You can set the themes on your page either declaratively or programmatically. To set a theme declaratively, add the Theme attribute to the Page directive:
<%@ Page Language="VB" AutoEventWireup="true"
CodeFile="Default.aspx.vb" Inherits="Default_aspx" Theme="Dark Blue"%>
You can also set the theme programmatically, either by hard coding it or (even better) by setting it from the user’s profile.
StyleSheet themes are set by overriding theStyleSheetThemeproperty for the page. IntelliSense will help you with this. Open Welcome.aspx.vb and scroll to the bottom of the class. Type the word overrides and all the overridable members are shown. Start typing sty and IntelliSense will scroll to the property you want:StyleSheetTheme, as shown in Figure 12-48.

Figure 12-48. Overriding a method
Once IntelliSense finds the property you want, press Tab to accept it. Fill in the accessors, as shown in Example 12-22.
Example 12-22. Setting a StylesheetTheme property
Public Overrides Property StyleSheetTheme() As String
Get
If Profile.IsAnonymous = False And Profile.Theme IsNot Nothing Then
Return Profile.Theme
Else
Return "Dark Blue"
End If
End Get
Set(ByVal value As String)
Profile.Theme = value
End Set
End Property
If you are going to set a customization theme programmatically, however, you must do so from thePreInitevent handler for the page,* because the theme must be set before the controls are created. APreInitevent handler is shown in Example 12-23.
Example 12-23. Welcome page PreInit event handler
Protected Sub Page_PreInit( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.PreInit
If Profile.IsAnonymous = False Then
Page.Theme = Profile.Theme
End If
End Sub
Setting the theme inPreInitcreates a bit of a difficulty when you want to allow the user to change the theme at runtime. If you create a control that posts the page back with a new theme, thePreInit code runs before the event handler for the button that changes the theme, and so by the time the theme is changed, the controls have already been drawn.
* The pre-init event is new in Visual Basic 2005.
To overcome this you must, unfortunately, refresh the page again. An alternative is to set the themes in another page. For example, add two buttons to the ProfileInfo.aspx page (at the bottom of the table at the bottom of the page). Set the properties of the first button to:
ID="ThemeBlue" Text="Dark Blue" OnClick="Set_Theme"
Set the properties of the second button to:
ID="ThemePsychedelic" Text="Psychedelic" OnClick="Set_Theme"
Notice that the two buttons share a single Click event handler,Set_Theme, shown in Example 12-24. An easy way to have Visual Studio 2005 set up that event handler for you is to switch to Design view and click on one of the buttons. Click on the lightning bolt in the Properties window to go to the events, and double-click on theSet_Themeevent. You are now ready to implement the event handler. You’ll cast the sender to the button and check its text, setting the theme appropriately.
Example 12-24. Common Click event handler for ThemeBlue and ThemePsychedelic buttons
Protected Sub Set_Theme( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ThemePsych.Click
Dim btn As Button = CType(sender, Button)
If btn.Text = "Psychedelic" Then
Profile.Theme = "Psychedelic"
Else
Profile.Theme = "Dark Blue"
End If
End Sub
When the user is not logged on, the Welcome page’s default theme will be used. Once the user sets a theme in the profile, that theme will be used when you return to the Welcome page. Create skins for your two themes and then run the application to see the effect of applying the themes.
Next: Using Named Skins >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter 12 of the book Programming Visual Basic 2005, written by Jesse Liberty (O'Reilly, 2005; ISBN: 0596009496). Check it out today at your favorite bookstore. Buy this book now.
|
|