Personalize with Complex Types

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 CheckBoxList just above the row with the Save button, as shown in Figure 12-37.

Adding checkboxes to profile

Figure 12-37. Adding checkboxes to profile

Modify the Save button handler to add the selected books to the profile, as shown in Example 12-14.

Example 12-14. Code to modify Save button Click event handler

Profile.CHOSENBOOKS = New System.Collections.Specialized.StringCollection()
For Each item As ListItem In Me.cblChosenBooks.Items
    If item.Selected Then
        Profile.CHOSENBOOKS.Add(item.Value.ToString())
    End If
Next

Each time you save the books, you create an instance of the String collection, and you then iterate through the checked list boxes, looking for the selected items. Each ...

Get Programming Visual Basic 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.