Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Be a Purveyor of Personalizable Web Pages Using ASP.NET's Profile Service : Page 3

Learn how to create ASP.NET Web pages that please visitors by displaying data in their preferred language or by remembering user-entered values from one visit to the next.


advertisement
Storing Objects in Profile
So far I have limited discussion to the localization process. But I'll now focus my attention on the Profile properties:

<profile> <properties> <add name="language" type="string"/> <group name="Info"> <add name="DateSelected" type="System.DateTime"/> <add name="LastModified" type="System.DateTime"/> </group> </properties> </profile>

By default, the data type of a Profile property is String. Hence the following properties definitions are equivalent:

<add name="language" /> <add name="language" type="string"/>

However, you can use any data types supported in the .NET Framework. Moreover, you can also use your own custom data type (as long as it is serializable).



Consider the following example. Assume I have a class, Class1.vb, in my project:

<Serializable()> _ Public Class CombinedInfo Public DateSelected As DateTime Public LastModified As DateTime Public Language As String End Class

I can add a Profile property named combinedInfo of type CombinedInfo:

<profile> <properties> <add name="language" type="string"/> <group name="Info"> <add name="DateSelected" type="System.DateTime"/> <add name="LastModified" type="System.DateTime"/> </group> <add name="combinedInfo" type="CombinedInfo" serializeAs="Xml" /> </properties> </profile>

To use the Profile property, I can save an instance of the CombinedInfo class into the combinedInfo profile property:

Shared cinfo As New CombinedInfo '---set the value of the CombinedInfo object cinfo.Language = ddlLanguage.SelectedItem.Value cinfo.DateSelected = Calendar1.SelectedDate cinfo.LastModified = Now '---assign to the profile property Profile.combinedInfo = cinfo '---retrieve the profile property Dim info As CombinedInfo info = Profile.combinedInfo

Also note the serializeAs attribute. In the above example I set it to "Xml." This means that the CombinedInfo object would be serialized and persisted as an XML string. Alternatively, you can also use Binary serialization. Do note that for XML serialization only public members are serialized.

Examining the Data Persisted by the Profile Object
Before concluding this article, I'll examine the data that have been persisted by the Profile object discussed in the above example. As soon as the Profile object is used, an Access database file would be created in the Data folder under your project (default in Beta 1, unless you explicitly specify to use SQL Server for the Profile Provider).

In the ASPNetDB Access database, the aspnet_Profile table shows the information stored by the Profile object (see Figure 8).

Figure 8. Aspnet_Profile Table: The table shows the information stored by the Profile object.
There are a few interesting things to note here.
  • The PropertyNames field stores the Profile properties that have been saved with their corresponding values stored in the PropertyValuesString field.
  • The UserID associated with this Profile object corresponds to my login user name—"WINXP-Wei-Meng Lee" in this case. This is because I am using the default Windows authentication in my application. For the Profile object to work, you need a way to identify a user, either by his login ID (for Windows authentication), or for Forms authentication, you need to explicitly enable anonymous identification so that the ASP.NET runtime can use a GUID as a identifier for an anonymous user:

    <anonymousIdentification enabled="true"/> <profile> <properties> <add name="language" type="string" allowAnonymous="true" /> <add name="combinedInfo" type="CombinedInfo" serializeAs="Xml" allowAnonymous="true" /> </properties> </profile>

Up Close and Personal
In this article, I have illustrated several new features in ASP.NET 2.0:

  1. The use of Implicit Localization to allow Web pages to automatically localize based on the language preference set in IE.
  2. The use of Explicit Localization to allow users to choose their own desired language without needing to set the language preference in IE.
  3. The use of the Profile service in storing information input by your users, thereby allowing you to personalize your site.



Wei-Meng Lee is a Microsoft .NET MVP and co-founder of Active Developer, a training company specializing in .NET and wireless technologies. He is a frequent speaker and author of numerous books on .NET, XML, and wireless technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap