Personalization
Good web sites not only present content in a good appealing way but also know how to present the correct content to the right user. Personalized content means that the user is displayed the content he might be interested in based on his preferences and other known information about him.
This ability to display personalized content and track information about the user is known as Personalization. This personalization is provided in ASP.NET by the Profile service.
Profile service provides the ability to easily store and retrieve information about the user. Profile service stores and retrieves the data of the user and maps the data for the user. We are not concerned with manually storing and retrieving the user information as this task is performed by the ASP.NET Profile Service and we don't need to write any code for it.
To use Profile service in the web site we need to define the properties we want to store and retreive in web.config.In the following xml markup defined in the web.config we have added two new properties.
Type attribute defines the datatype of the Property .Since default is System.String we can omit the below type attribute.
<properties>
<add name="name" type="System.String" allowAnonymous="true" />
<add name="age" type="System.Int32" allowAnonymous="true"/>
</properties>
We can also define the Profile properties in Profile groups .Defining Profile properties in Profile groups serve to group logically related properties together and also to define two properties having same name in different groups. To define profile group we need to add the <group name ="name"> tag in the <properties> tag.If we had defined the address property inside the group named "user" we need to access the address property as Profile.User.address.
Based on this information in web.config ASP.NET automatically generates a class for us which gives us access to the Profile service which we can access in the web pages using the Profile property of the page class .In the following code in the code behind we are setting the values of the Profile properties we have defined earlier in web.config.
Following is the code we add in the Page Load event handler.
if (Profile.name != ""
&& Profile.address != "")
{
lblAddress.Text = Convert.ToString(Profile.address);
lblName.Text
= Profile.name;
}
else
{
lblName.Text
= "Name not set";
lblAddress.Text = "Address not
set";
}
On the button click event handler we add the following code which sets the Profile's name and address properties.
Profile.address = txtAddress.Text;
Profile.name = txtName.Text;
Following is the markup we have added to the aspx page.
Name<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Address<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Set Profile" />
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblAddress" runat="server" Text="Label"></asp:Label>
Now if we input some text in the name and address and click the set profile button the profile properties are set.
So if after adding the above code we excute the application we will see "Name not set" and "Address not set" messages but after entering the name and address and clicking on Set Profile button we will see the Name and Address we had set previously.This information will persist whenever we open the web page again (between sessions) and also between application restarts.Following are the screenshots before and after we set the profile properties.
Before Profile Properties are set
After Profile Properties are set
The changes we made to the Profile are saved after the Page End which is the last stage in the Page Life cycle.
ProfileCommon Class
Whenever we add a property in web.config and save it ASP.NET creates a class for us and this class is named ProfileCommon.This class is accessible in the Page class through the Profile property. ProfileCommon class is derived from another class ProfileBase.ProfileBase does the work of interacting with the Provider we had configured in the web.config.
Provider does the work of accessing Profile data in database for storage and retrieval.Provider used by default is SqlProfileProvider.
We can configure the database in the web.config so the provider use the different database.