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.
<%@ 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" Runat="server" /> </td> </tr> <tr> <td>Last Name: </td> <td style="width: 193px"> <asp:TextBox ID="txtLastName" Runat="server" /></td> </tr> <tr> <td>Phone number: </td> <td style="width: 193px"> <asp:TextBox ID="txtPhone" Runat="server" /> </td> </tr> <tr> <td>BirthDate</td> <td style="width: 193px"> <asp:TextBox ID="txtBirthDate" Runat="server" /> </td> </tr> <tr> <td> <asp:CheckBoxList ID="cblChosenBooks" runat="server"> <asp:ListItem>Programming C#</asp:ListItem> <asp:ListItem>Programming ASP.NET</asp:ListItem> <asp:ListItem>Programming .NET Apps</asp:ListItem> <asp:ListItem>Agile Software Development</asp:ListItem> <asp:ListItem>UML2 For Dummies</asp:ListItem> <asp:ListItem> Object Oriented Design Heuristics </asp:ListItem> <asp:ListItem>Design Patterns</asp:ListItem> </asp:CheckBoxList> </td> </tr> <tr> <td> <asp:Button ID="save" Text="Save" Runat="server" OnClick="save_Click" /> </td> <td style="width: 193px"></td> </tr> </table> </div> </form> </body> </html>
Modify the Save button handler to add the selected books to the profile:
protected void save_Click(object sender, EventArgs e) { if (Profile.IsAnonymous == false) { Profile.lastName = this.lastName.Text; Profile.firstName = this.firstName.Text; Profile.phoneNumber = this.phone.Text; DateTime birthDate = Convert.ToDateTime(this.birthDate.Text); Profile.birthDate = birthDate; Profile.CHOSENBOOKS = new System.Collections.Specialized.StringCollection( ); foreach (ListItem li in this.cblChosenBooks.Items) { if (li.Selected) { Profile.CHOSENBOOKS.Add(li.Value.ToString( )); } } } Response.Redirect("Welcome.aspx"); }
To make your code a bit easier to maintain, you want to have the selected values (e.g., name, phone, selected books) pre-filled when you return to the profile editing page, so you'll implement a bit of code on Page_Load to get the initial values from the Profile object:
protected void Page_Load(object sender, EventArgs e) { if (Profile.IsAnonymous == false) { this.lastName.Text = Profile.lastName; this.firstName.Text = Profile.firstName; this.phone.Text = Profile.phoneNumber; this.birthDate.Text = Profile.birthDate.ToShortDateString( ); if (Profile.CHOSENBOOKS != null) { foreach (ListItem li in this.cblChosenBooks.Items) { foreach (string profileString in Profile.CHOSENBOOKS) { if (li.Text == profileString) { li.Selected = true; } // end if text matches } // end for each profile string } // end for each item in list } // end if chosen books not null } // end if not anonymous } // end page load
|
To confirm that this data has been stored, add a list box ( lbBooks ) to the Welcome.aspx file:
<tr> <td> <asp:ListBox ID="lbBooks" runat="server" /> </td> </tr>
Update Welcome.aspx.cs.Page_Load to reflect the contents of the chosen books:
protected void Page_Load(object sender, EventArgs e) { if (Profile.UserName != null && Profile.IsAnonymous == false) { this.pnlInfo.Visible = true; this.lblFullName.Text = Profile.firstName + " " + Profile.lastName; this.lblPhone.Text = Profile.phoneNumber; this.lblBirthDate.Text = Profile.birthDate.ToShortDateString( ); this.lbBooks.Items.Clear( ); if (Profile.CHOSENBOOKS != null) { foreach (string bookName in Profile.CHOSENBOOKS) { this.lbBooks.Items.Add(bookName); } } } else { this.pnlInfo.Visible = false; }
When you click on the profile, you will have the option to add (or change) the books associated with your profile, as shown in Figure 13-7.
When you click Save and return to the welcome page, your saved profile information will be reflected, as shown in Figure 13-8.