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. Figure 12-37. Adding checkboxes to profileModify 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 handlerProfile.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 selected item is added to the string collection within the profile (the CHOSENBOOKS property). You also need to override Page_Load so that this page will open with the user's profile information updated, as shown in Example 12-15. Example 12-15. Modified ProfileInfo.aspx.vbPartial Class ProfileInfo Inherits System.Web.UI.Page Protected Sub save_Click( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles save.Click If Profile.IsAnonymous = False Then Profile.lastName = Me.lastName.Text Profile.firstName = Me.firstName.Text Profile.phoneNumber = Me.phone.Text Profile.birthDate = CType(Me.birthDate.Text, System.DateTime) 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 End If Response.Redirect("Welcome.aspx") End Sub Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack And Profile.UserName IsNot Nothing Then If Profile.IsAnonymous = False Then Me.lastName.Text = Profile.lastName Me.firstName.Text = Profile.firstName Me.phone.Text = Profile.phoneNumber Me.birthDate.Text = Profile.birthDate.ToShortDateString( ) End If If Profile.CHOSENBOOKS IsNot Nothing Then For Each theListItem As ListItem In Me.cblChosenBooks.Items For Each theProfileString As String In Profile.CHOSENBOOKS If theListItem.Text = theProfileString Then theListItem.Selected = True End If Next Next End If End If End Sub End Class Each time you navigate to the Profile page, the values are updated from the existing profile (if any) in Page_Load and you are free to change them and save the new values, as shown in Figure 12-38. Figure 12-38. Profile Information page with CheckBoxListTo confirm that this data has been stored, add a ListBox (name it lbBooks) to the pnlInfo panel you added to Welcome.aspx page, as shown in Figure 12-39. Figure 12-39. ListBox added to panelBind the ListBox to the collection in the profile, as shown in Example 12-16. Example 12-16. Modified Page_Load in Welcome.aspx.vb Protected Sub Page_Load( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack And Profile.UserName IsNot Nothing Then Me.pnlInfo.Visible = True If Profile.IsAnonymous = False Then Me.lblFullName.Text = Profile.firstName & " " & Profile.lastName Me.lblPhone.Text = Profile.phoneNumber Me.lblBirthDate.Text = Profile.birthDate.ToShortDateString( ) End If If Profile.CHOSENBOOKS IsNot Nothing Then For Each bookName As String In Profile.CHOSENBOOKS Me.lbBooks.Items.Add(bookName) Next End If Else Me.pnlInfo.Visible = False End If End Sub When you click Save in the Profile page and return to the Welcome page, your saved profile information is reflected, as shown in Figure 12-40. Figure 12-40. Welcome page with chosen books |