| Personalization |
| |
| Personalization Property Creation |
| |
| ASP.NET 2.0 introduces a new object named profile that simplifies the steps involved in tracking personalization information. You can create personalization properties that are used only by authenticated users, and also some that users can utilize. To do this, you need to store the data in a database. |
| |
| In an ASP.NET profile, information is stored in a persistent format and is associated with an individual user. A profile is simple a set of user related data and information that gets stored in a data source. |
| |
| Defining the User Profiles |
| |
| To add data to the user's profile, you define a user profile within the application root web.config file. You cannot create a web.config file containing a profile section in an application subfolder. This means that you can have only one profile element declaration for one Web application. The following declaration in the web.config contains a simple profile definition: |
| |
<profile>
<properties>
<add name="FirstName" />
<add name="LastName" />
<add name="MobileNumber" />
<add name="DOB" type="System.DateTime"/>
</properties>
</profile> |
| |
| After defining a profile, whenever someone requests a page from the Web site ASP.NET automatically generates a class named ProfileCommon that corresponds to the profile definition. This class is stored in the Temporary ASP.NET Files directory, and an instance of this class is made available through the profile property of the HttpContext object. |
| |
| Grouping of properties |
| |
| Sometimes you may want to group the related properties into a profile group to make your work easy with them from your ASP.NET page or to convey some type of business concern. For example, a group of related properties may represent user preferences, where another group may represent billing information. In ASP.NET 2.0 properties can be grouped together using the <group> tag. |
| |
<profile>
<properties>
<group name="FullName">
<add name="FirstName? type="System.String?/>
<add name="LastName? type="System.String?/>
</group>
<group name="OtherInfo">
<add name="MobileNumber" type="System.String?/>
<add name="DOB" type=" System.DateTime"/>
</group>
</properties>
</profile> |
| |
| This results the Profile API to create storage for four member of information (First and Last name, Mobile number, and DOB). The default storage type is string, but here we are storing the DOB as a datetime object. To declare data types other than string, you must define the type for that profile. |
| |
| Working with User Profiles |
| |
| Once you have the profiles declared, the next step is to access the profile object from within the ASP.NET page so that you can set or get the values stored in the profile properties. When you click Save, the event handler is fired and you may store the data to the Profile object. The Profile object was automatically instantiated for the current user, and so the data you assign here is properly associated with the current user ID. |
| |
Sample Code:
void save_Click(object sender, EventArgs e)
{
Profile. FirstName = this. FirstName.Text;
Profile. LastName = this. LastName.Text;
Profile. MobileNumber = this. MobileNumber.Text;
Profile. DOB = Convert.ToDateTime(this. DOB.Text);
Response.Redirect("Default.aspx");
} |
| |
| Note: To set values using grouped profiles. |
| |
void save_Click(object sender, EventArgs e)
{
Profile. FullName.FirstName = this. FirstName.Text;
Profile. FullName.LastName = this. LastName.Text;
Profile. OtherInfo.MobileNumber = this. MobileNumber.Text;
Profile. OtherInfo.DOB = Convert.ToDateTime(this. DOB.Text);
Response.Redirect("Default.aspx");
} |
| |
| The Profile object has properties that correspond to the properties you added in Web.config. To test that the Profile object has in fact stored this date, you'll add a panel to the default page: |
| |
Sample Code:
<asp:Panel ID="pnlInfo" Runat="server" Visible="False" Width="422px" Height="63px">
<br />
<table width="100%">
<tr>
<td>
<asp:Label ID="lblFullName" Runat="server" Text="Full name unknown">
</asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblMobile" Runat="server" Text="Mobile Number unknown">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblBirthDate" Runat="server" Text=" Date Of Birth unknown">
</asp:Label>
</td>
</tr>
</table>
</asp:Panel> |
| |
| The panel contains a table with three rows, and each row has a label that is initialized to say that the value is unknown (this is not normally needed, but is included here to ensure that the data you see was in fact retrieved from the Profile object). When the page is loaded, you check to see if you have Profile data for this user and, if so, you assign that data to the appropriate controls. |
| |
Sample Code:
if (Profile.UserName != null && Profile.IsAnonymous == false )
{
this.lblFullName.Text = "Full name: " + Profile.firstName + " " + Profile.lastName;
this.lblPhone.Text = "Phone: " + Profile.PhoneNumber;
this.lblBirthDate.Text = "Born: " + Profile.DOB.ToShortDateString();
this.pnlInfo.Visible = true;
}
else
{
this.pnlInfo.Visible = false;
}
Note: To get value of groped user profile.
if (Profile.UserName != null && Profile.IsAnonymous == false )
{
this.lblFullName.Text = "Full name: " + Profile. FullName.firstName + " "
+ Profile. FullName.lastName;
this.lblPhone.Text = "Phone: " + Profile. OtherInfo.PhoneNumber;
this.lblBirthDate.Text = "Born: " + Profile. OtherInfo.DOB.ToShortDateString();
this.pnlInfo.Visible = true;
}
else
{
this.pnlInfo.Visible = false;
} |
| |
| Notice that you convert the datetime to a string for easy display in the label. |
| |
| How Profile Properties Are Stored |
| |
| You may be surprised that where exactly the profile properties FirstName, LastName, MobileNumber and DOB are stored in the above example. By default, ASP.NET 2.0 comes with two profile providers: SQL Server Express and SQL Server. The SQL Server Express provider is used as default provider. |
| |
| If you refresh your project listing in Solution Explorer, you will notice a database file called ASPNETDB.MDF within the App_Data folder. If you double click on this database, you will see that the database is opened through the Server Explorer view. Within this database, you will see a table called aspnet_Profile. |
| |
|
| |