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.
Up Close and Personal
In this article, I have illustrated several new features in ASP.NET 2.0:
- The use of Implicit Localization to allow Web pages to automatically localize based on the language preference set in IE.
- The use of Explicit Localization to allow users to choose their own desired language without needing to set the language preference in IE.
- The use of the Profile service in storing information input by your users, thereby allowing you to personalize your site.