Friday, September 23, 2005

Theme & Personalization in .Net 2

If you want provide the feature to let user select their own theme. You may need to write your own ProfileProvider and save the setting in users table. Here are my study in Theme & Personalization.
1. Theme
Theme is for the look and feel of web pages. Themes can use CSS files also. Just put the CSS file under specific theme folder, the page will use it.
File structure:
[Web App Root]\App_Themes\Blue\blue.shin
Then you can appay this skin in several way:

//In Single Page

//In Page_PreInit Event
void Page_PreInit(object sender, System.EventArgs e)
{ this.Theme = "Blue";}
2. Personalization
Let's save the Theme setting in Profile. In web.config file, add
〈 profile defaultProvider="myProfileProvider"〉
〈 providers〉
〈 add name="myProfileProvider" type="Corp.Framework.Shared.Security.myProfileProvider, Corp.Framework.Shared" connectionStringName="ConnID"/〉
〈 /providers〉
〈 properties〉
〈 add name="Theme" defaultValue="Blue"/〉
〈 /properties〉
〈 /profile〉

//In Page_PreInit Event, you load user's setting from Profile
void Page_PreInit(object sender, System.EventArgs e)
{
this.Theme = Profile.Theme;
//Page.Theme = (string)HttpContext.Current.Profile["Theme"];
}
3. myProfileProvider
myProfileProvider inherits from the SettingsProvider abstract class, which inherits from the ProviderBase abstract class.
class myProfileProvider : ProfileProvider
{
#region Private Member
private string _applicationName;
#endregion // Private Member

#region Constructor
///
/// Default Constructor
///

public myProfileProvider()
: base()
{
}
#endregion // Constructor
#region Initialize
///
/// Initialize the Profile Provider Class
///

/// Provider name
/// NameValueCollection containing the provider configuration settings
public override void Initialize(string name, NameValueCollection config)
{
}
#endregion //Initialize

#region Get Application Name
public override string ApplicationName
{
get { return _applicationName; }
set { _applicationName = value; }
}
#endregion
#region GetPropertyValues
///
/// Get Property Values
///

/// Context
/// Collection of Property Value
public override SettingsPropertyValueCollection
GetPropertyValues(SettingsContext context,
SettingsPropertyCollection ppc)
{
// In Web form, user login by their email, here username pass the email
string email = (string)context["UserName"];
bool isAuthenticated = (bool)context["IsAuthenticated"];

SettingsPropertyValueCollection svc =
new SettingsPropertyValueCollection();

foreach (SettingsProperty prop in ppc)
{
SettingsPropertyValue pv = new SettingsPropertyValue(prop);
switch (prop.Name)
{
case "Theme":
pv.PropertyValue = GetTheme(email, isAuthenticated, (string)prop.DefaultValue);
break;
default:
throw new ProviderException("Unsupported property.");
}
svc.Add(pv);
}
UpdateActivityDates(email, isAuthenticated, true);
return svc;
}
#endregion //GetPropertyValues

#region SetPropertyValues
///
/// Set Perperty Values
///

/// Context
/// Collection of Property value
public override void SetPropertyValues(SettingsContext context,
SettingsPropertyValueCollection ppvc)
{

}
#endregion //SetPropertyValues


#region UpdateActivityDates
/// Updates the LastActivityDate and LastUpdatedDate values
/// when profile properties are accessed by the
/// GetPropertyValues and SetPropertyValues methods.
/// Passing true as the activityOnly parameter will update
/// only the LastActivityDate.
///

/// Current user's email
/// is Authenticated
/// Activity
private void UpdateActivityDates(string email, bool isAuthenticated, bool activityOnly)
{
DateTime activityDate = DateTime.Now;
}
#endregion //UpdateActivityDates


#region GetTheme
///
/// Retrieves theme from the database during the call to GetPropertyValues.
///

/// Current user's email
/// is Authenticated
///
private string GetTheme(string email, bool isAuthenticated, string defaultTheme)
{
if (!isAuthenticated)
{
return defaultTheme;
}
if (null == email)
throw new ArgumentNullException("email");
// check if username is empty
if (email.Length == 0)
throw new ArgumentException("Argument is empty", "username");
try
{
// create the object manager
IUsersManager usersManager = ManagerFactory.CreateManager(Utilities.PortalConnStringID);
// Validate
IUsers user = usersManager.RetrieveByemail(email);
if (user.IsEmpty)
return defaultTheme;

if (string.IsNullOrEmpty(user.theme))
return defaultTheme;
else
return user.theme;
}
catch
{
throw;
}

}
#endregion
}

9 comments:

Anonymous said...

What a great blog. Keep up the good work. How do you like my blog
about webex mac os x:-)

Anonymous said...

Hey, you have a great blog here! I'm definitely going to bookmark you!

I have a microsoft gdi detection site. It pretty much covers microsoft gdi detection related stuff.

Come and check it out if you get time :-)

Anonymous said...

For more wedding ideas information please visit. It can be very uncomfortable to
see your wedding guests crammed together to dance in a tiny
space, so make sure there is enough room prior to the event.
Some brides simply cannot find anything which inspires them.


Check out my site ... simple wedding card design

Anonymous said...

But the question remains - how can a person find and
make a right choice of a good interior design school to study at.
Not to mention that I have a couple of sofas as part of my modern furniture.
The reception and entrance area should be fitted with
bright lighting fixtures.

my page; Free Interior Painting Ideas

Anonymous said...

The burden that obesity places on your family is overwhelming at times.
Still another way of curing obesity is to eat low carbohydrate, low fat, high protein
diets. Control your cravings Most of the things we crave for have too much calories
e.

My webpage green tea weight loss reviews

Anonymous said...

Benefit from a wide range of choices though picking the living space furnishings, in the event the walls of the
living room have pale or light shades like beige or white.
f you are feeling inventive and have terrific self-confidence within your artistic talents then this tip is for you.
Assume of your space as an outfit and accessorize your room inside your thoughts.



Take a look at my site :: baby girls room decoration ideas

Anonymous said...

If you would like to increase your knowledge only keep
visiting this website and be updated with the most up-to-date information
posted here.

my web page - listening-for-a-change.blogspot.com

Anonymous said...

I blog often and I seriously appreciate your content.
The article has really peaked my interest. I'm going to bookmark your site and keep checking for new information about once a week. I subscribed to your RSS feed too.

My web blog :: quantrim truth

Anonymous said...

I got this website from my pal who told me on the topic of this web site and at the
moment this time I am browsing this website and reading very informative articles or reviews at this time.


Here is my site; reviewed quantrim - twitter.ehl.ru -