Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [157]

By Root 5624 0
/>

type="System.Collections.Specialized.StringCollection" />

In the preceding code snippet, anonymous users can pick up the European metrics but cannot modify the way in which temperatures are displayed nor add their favorite locations.

Accessing Profile Properties


In a Web site project, the Page object features an extra Profile property added by the system via a partial class during the dynamic compilation step. Before the request begins its processing cycle, the Profile property of the page is set with an instance of the profile class created out of the content in the web.config file.

When the page first loads, profile properties are set to their default values (if any) or they are empty objects. They are never null. When custom or collection types are used to define properties, assigning default values might be hard. The code just shown defines a string collection object—the property Locations—but giving that a default value expressed as a string is not supported. At run time, though, the Locations property won’t be null—it will equal an empty collection. So how can you manage default values for these properties?

Properties that don’t have a default value can be initialized in the Page_Load event when the page is not posting back. Here’s how you can do that:

if (!IsPostBack)

{

// Add some cities to the Locations property

if (Profile.Locations.Count == 0) {

Profile.Locations.Add("London");

Profile.Locations.Add("Amsterdam");

}

}

In a Web site project, the personalization data of a page is all set when the Page_Init event fires. However, when the Page_PreInit event arrives, no operation has been accomplished yet on the page, not even the loading of personalization data.

In a WAP project, if you opt for a strongly typed approach, you have no way to assign a default value to properties. The only workaround, obviously, is dealing with defaults right in the getter method of each property. Here’s an example:

[SettingsAllowAnonymous(true)]

public String TempSystem

{

get

{

var current = (String) HttpContext.Current.Profile.GetPropertyValue("TempSystem");

if (String.IsNullOrEmpty(current))

return "F";

return (String) current;

}

set { HttpContext.Current.Profile.SetPropertyValue("TempSystem", value); }

}

In a Web site project, the personalization data of a page is available only on demand—precisely, the first time you access the profile object.

Let’s consider some sample code that illustrates the power of the user profile API.

Note

The personalization data of a page is all set when the Page_Init event fires. However, when the Page_PreInit event arrives, no operation has been accomplished yet on the page, not even the loading of personalization data.

User Profiles in Action


Suppose you have a page that displays information according to user preferences. You should use the user profile API only to store preferences, not to store sensitive data. Losing the profile information should never cause the user any loss of money or serious inconvenience. Here’s the code you might have at the startup of the page request. The page first grabs in some way some weather-related information and then displays it as configured by the user:

protected void Page_Load(Object sender, EventArgs e)

{

if (!IsPostBack)

{

var info = GrabWeatherInfo();

DisplayData(info);

}

}

private static WeatherInfo GrabWeatherInfo()

{

...

}

private void DisplayData(WeatherInfo info)

{

// Type-safe solution for Web Application projects

// (reusing the YourApp.UserProfile wrapper class discussed earlier)

// Get profile information from the underlying repository

var profile = YourApp.UserProfile.GetUserProfile();

// Metric system

var speedFormat = "{0} mph";

if (profile.UseEuroMetricSystem)

speedFormat = "{0} kmh";

var speedText = String.Format(speedFormat, info.WindSpeed);

// Temperature

var tempText = String.Format("{0} {1}", info.Temperature, profile.TempSystem);

lblWindSpeed.Text = speedText;

lblTemperature.Text = tempText;

// The sample

Return Main Page Previous Page Next Page

®Online Book Reader