O'Reilly logo

Programming ASP.NET, 3rd Edition by Dan Hurwitz, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required