Personalizing with Complex Types

While personalizing a site for your users is terrific, to make a useful commercial site, you often have to store complex user-defined types (classes) or collections.

In the next exercise, you’ll edit the web.config file to add a collection of strings called CHOSENBOOKS. Doing so will allow the user to choose one or more books and have those choices stored in the user’s profile.

Add a line to web.config for your new property:

    <profile>
      <properties>
        <add name="lastName" />
        <add name="firstName" />
        <add name="phoneNumber" />
        <add name="birthDate" type="System.DateTime"/>
        
        <add name="CHOSENBOOKS"
            type="System.Collections.Specialized.StringCollection" />
      </properties>
    </profile>

To see this collection at work, edit the page ProfileInfo.aspx, inserting a row with a checkbox list just above the row with the Save button. While you are there, let’s rename the text boxes to our normal naming scheme and fix the corresponding code. The complete content file is shown in Example 13-2.

Example 13-2. Revised ProfileInfo.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ProfileInfo.aspx.cs"
   Inherits="ProfileInfo_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <table>
            <tr>
                <td>First Name: </td>
                <td style="width: 193px">
                   <asp:TextBox ID="txtFirstName" ...

Get Programming ASP.NET, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.