New Document
ASP.NET - 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.

Web sites are designed for repeated visits from the users. Personalization allows a site to remember its user and his/her information details and presents an individualistic environment to each user.

ASP.Net provides services for personalizing a web site to suit a particular client's taste and preference.

Understanding Profiles:

ASP.Net personalization service is based on user profile. User profile defines the kind of information about the user that the site will need, for example, name, age, address, date of birth, phone number etc.

This information is defined in the web.config file of the application and ASP.Net runtime reads and uses it. This job is done by the personalization providers.

The user profiles obtained from user data is stored in a default database created by ASP.Net. You can create your own database for storing profiles. The profile data definition is stored in the configuration file web.config.

Example:

Let us create a sample site, where we want our application to remember user details like name, address, date of birth etc. Add the profile details in the web.config file within the <system.web> element.

<profile>
	<properties>
	  <add name="Name" type ="String"/>
	  <add name="Birthday" type ="System.DateTime"/>
	  <group name="Address">
		<add name="Street"/>
		<add name="City"/>
		<add name="State"/>
		<add name="Zipcode"/>
	  </group>
	</properties>
  </profile>
</system.web>
</configuration>

When the profile is defined in the web.config file, the profile could be used through the Profile property found in the current HttpContext and also available via page.

Add the text boxes to take the user input as defined in the profile and add a button for submitting the data:

Personalization

Update Page_load to display profile information :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!this.IsPostBack)
      {
      ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);
      if (pc != null)
      {
         this.txtname.Text = pc.Name;
         this.txtaddr.Text = pc.Address.Street;
         this.txtcity.Text = pc.Address.City;
         this.txtstate.Text = pc.Address.State;
         this.txtzip.Text = pc.Address.Zipcode;
         this.Calendar1.SelectedDate = pc.Birthday;
       }
       }
    }
}

Write the following handler for the Submit button, for saving the user data into the profile:

protected void btnsubmit_Click(object sender, EventArgs e)
{
  ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);
  if (pc != null)
  {
     pc.Name = this.txtname.Text;
     pc.Address.Street = this.txtaddr.Text;
     pc.Address.City = this.txtcity.Text;
     pc.Address.State = this.txtstate.Text;
     pc.Address.Zipcode = this.txtzip.Text;
     pc.Birthday = this.Calendar1.SelectedDate;
     pc.Save();
  }
}

Observe: When the page is executed for the first time, the user information need to be entered. However, next time the user details would be automatically loaded.

Attributes for the <add> Element

Apart from the name and type attributes that we have used, there are other attributes to the <add> element. Following table illustrates some of these attributes:

AttributesDescription
nameThe name of the property.
typeBy default the type is string but it allows any fully qualified class name as data type.
serializeAsThe format to use when serializing this value.
readOnlyA read only profile value cannot be changed, by default this property is false.
defaultValueA default value that will be used if the profile does not exist or does not have information.
allowAnonymousA Boolean value indicating whether this property can be used with the anonymous profiles.
ProviderThe profiles provider that should be used to manage just this property.
Anonymous Personalization:

Anonymous personalization allows the user to personalize the site before identifying themselves. For example, Amazon.com allows the user to add items in the shopping cart before they log in. To enable this feature, the web.config file could be configured as:

<anonymousIdentification enabled ="true"
cookieName=".ASPXANONYMOUSUSER"
cookieTimeout="120000"
cookiePath="/"
cookieRequiresSSL="false"
cookieSlidingExpiration="true"
cookieprotection="Encryption"
coolieless="UseDeviceProfile"/>


Previous                                                                                                                                                       Next

Back to Top