Sponsored Links
ASP.NET 2.0 Tutorials
- Advanced Site Functionality - ASP.NET 2.0
- ASP.NET : Dynamic Image control
- ASP.NET 2.0 Creating Web Wizards
- Displaying Master-Detail Data on Separate Pages in ASP.NET 2.0
- ASP.NET Displaying Master-Detail Data on the Same Page
- ASP.NET DataBound Controls - Details View
- ASP.NET : Using a Grid to Display Detail Information
- ASP.NET 2.0 : Adding Sorting and Paging in GridView
- ASP.NET 2.0 Tutorials : GridView Filtering
- ASP.NET GridView Control
- ASP.NET 2.0 Training : Data Bound Controls
- ASP.NET 2.0 Free Tutorials : SqlDataSource Control
- ASP.NET 2.0 Training : Data Source Object Model
- ASP.NET 2.0 Free Training : Setting Application-Level Caching
- ASP.NET 2.0 Tutorials : Configuring Page-Level Caching
- ASP.NET 2.0 Free Tutorials : Managing Membership and Roles
- ASP.NET 2.0 Free Tutorials : State Management And Caching in ASp.net 2.0
- Forms Authentication in ASP.NET 2.0
- ASP.NET 2.0 Training : ASP.NET Security
- ASP.NET 2.0 Training : Customizing the Session State Mechanism
Tutorials
ASP.NET 2.0ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes
Table of Contents
ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes
ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes - Page 2ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes
ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes
In this tutorial you will learn about Personalization - User Profiles and Themes, Inbuilt providers in ASP.NET 2.0 and also Create and execute a Shopping cart application with Personalization features.
Surfers would love to see on the website the content which is customized to their needs. ASP.NET offers a personalization feature which enables users to do just that. Personalization is a storage system that manages all the data related to users of the application. It integrates seamlessly with SQL server or Microsoft Access or any other database that developer’s wishes to use. A little code enables the creation of modular pages where personalization can manage the session’s services and the user can return to the screen at a later time to find the controls in the position he had left them. He can add controls from the catalog or remove a control and store it in the catalog for later use. This is called the persistent storage of structured data using a friendly and type safe API.
In ASP.NET personalization is implemented as user profiles and themes. These are complementary to each other. The model of personalized data is defined by the application and it is parsed at runtime by compiling that model into a class. The data in the personalized class corresponds to information specific to the current user. The internal working of the software is masked from both the user and the developer but the loading and saving of personalized data is completely transparent to the user.
Personalization:
In ASP.NET 2.0 Personalization is activated by default and is available to all authenticated users. However, before actually using personalization it is important to specify the data that needs to the be saved for each user. The profile of the user has to be defined in the < personalization > section of the web.config file. Individual properties may be placed and the profile properties can be defined by using the < property > tags.
< ?xml version= “1.0”
< configuration >
.............
< system.web >
.............
< profile >
< properties >
.............< add name="Nickname" / >
< / properties >
< / profile >
.............
< / system.web >
< / configuration >
In the source code file now the IntelliSense kicks in and you can call this.Profile. and the property is displayed in the drop down box. This is because a corresponding typed class is generated the moment the profile was defined. It was then compiled and integrated into the website’s assembly.
The current profile can be caught either by using the object model of the current page or through the HttpContext class. The personal data store is returned through the ‘Profile’ property.
Personalization is based on the provider model. Two providers are inbuilt into the ASP.NET 2.0—one for Microsoft Access and another for SQL Server. By default the profile is stored in the Microsoft Access database aspnetdb.mdb. The profile information is stored in direct relationship to the membership data. The GetProfile() method enables access to other profiles in the database. The user name has to be passed as a parameter to the method and the HttpPersonalization instance is returned with the data stored in the profile.
HttpPersonalization profile=this.Profile.GetProfile(“xxx”);
String name= Profile.Nickname;
It is possible to write into the data and the same can be saved if it is done explicitly by calling the CommitSettings() method to commit the data to the data source.
All properties of the profile are saved as strings by default. However other objects can be placed in the profile if they are serializable. DateTime, for instance can be stored by specifying a class name of the desired data type via the attribute type while defining the profile properties. It will read as
< Property name=”birthdate” type=”System.DateTime” / >
In the above example the complete class name of the data type including the corresponding namespace such as System.DateTime has been specified. However if a static value has to be used, it can be added with the attribute defaultValue.
The profile created can now be edited using a new page editprofile.aspx by each user in a type safe manner. We can see how this is done a little later in this tutorial.
Complex data types can also be stored in the profile from the base class library. The only precondition is that these data types should be serializable. To define the type the class’s full name and its namespace have to be specified. Though all values are serialized as string by default, property values are stored next to each other and the values are distinguished by their start and end positions.
PropertyNames: Username: X:0:1: Theme:X:7:22:
PropertyValuesString:XXX
However, string serialization is not considered advisable for reference types. The XML or binary format is more acceptable. The desired string can be assigned with the desired setting to the SerializeAs attribute. For instance a StringCollection can be serialized in the database as XML and will be available through the property name “xxx” that is serialized. Look at the example below to understand the concept better.
< personalization >
< profile >
< add name=”MyName” / >
< add name= “Birthdate” type= “System.DateTime” / >
< add name = “mycollections”
type= “System.Collections.Specialized.StringCollection” serializeAs=”Xml” / >
< / profile >
< / personalization >
This new profile property of the page and the HttpContext classes can now store ‘mycollections’. The URL of the current page can be dropped into the collection through a LinkButton located on the master page. The code for this button would look like this:
Void LinkButton_Click(object sender, System.EventArgs e)
{
HttpPersonalization profile = (HttpPersonalization) this.Context.Profile;
Profile.mycollections.Add(this.Request.Url.ToString());
It must be noted in this context that the master page does not allow type safe access to custom profiles. The HttpContext class is used to redeliver the profile through the HttpPersonalizationBaseClass and give access to the custom properties. Individual data also can be stored in a profile. The condition, again being serialization. We will see how this is done when we come to the practical half of the tutorial.
Anonymous users are supported in ASP.NET 2.0. This feature will have to be activated and custom properties of such users can be saved even if they are anonymous. How is this done? The profile property features have to be activated in the Web.config file. Enter the following code in the Web.config file
< configuration >
< system.Web >
< anonymousIdentification enabled= “ture” / >
< / system.web >
< / configuration >
Each property in the profile can be defined using the allowAnonymous attribute. If this property is not set, the properties will not be stored. We will see how the anonymous user’s profiles are stored when we look at the shopping cart example a little later in this tutorial.
Anonymous user profiles can be migrated to Authenticated user profiles by using the MigrateAnonymous event of the class PersonalizationModule. This is done in the global.asax file which is usually added to the website to fulfill this function.
Using the personalization features of ASP.NET 2.0 it is easy to store custom information belonging to the individual users. Simple strings or complex data structures can be added. Anonymous users can be identified during their visit to the site and later pinpointed if they repeat their visit.
Next Page: ASP.NET 2.0 Free Training : Personalization: User Profiles and Themes - Page 2
Comments
AGD said:
|
I am getting following error The type or namespace name 'HttpPersonalizationBaseclass' could not be found (are you missing a using directive or an assembly reference?) |
Sponsored Links
