Section 4.5. Personalize with Complex Types


4.5. Personalize with Complex Types

Although 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. The ASP.NET Web Site Administration Tool (WAT) makes that easy.


Note: Commercial sites often have to store complex user-defined types (classes) or collections for individual users (for example, shopping carts).

4.5.1. How do I do that?

Once again you need a new web site. Create one and call it ComplexPersonalization. Use the Copy Web Site Wizard to copy the previous lab to a new lab, or download the source from the previous lab and copy it to a new lab from the SitePersonalization folder.

In this lab you'll create the world's simplest shopping cart.

To create a complex profile property you'll need to edit the Web.config file. In this case, we'll add a collection of strings called CHOSENBOOKS that will allow the user to choose one or more books and have those choices stored in the 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, drag a CheckBoxList from the Visual Studio Toolbox onto the ProfileInfo page, which you will populate with the names of four books. Hand-populate this list by clicking the Items property and filling in the ListItems Collection Editor, or by adding the control by hand to the .aspx page using the following code:

<td style="width: 193px">     <asp:CheckBoxList  Runat="server" >         <asp:ListItem>Programming C#</asp:ListItem>         <asp:ListItem>Programming ASP.NET</asp:ListItem>         <asp:ListItem>Programming .NET Apps</asp:ListItem>         <asp:ListItem>Programming VB.NET</asp:ListItem>     </asp:CheckBoxList> </td>

Click the Save button; the handler will add the books to the profile:

void save_Click(object sender, EventArgs e) {    Profile.lastName = this.lastName.Text;    Profile.firstName = this.firstName.Text;    Profile.phoneNumber = this.phone.Text;    Profile.birthDate = Convert.ToDateTime(this.birthDate.Text);         Profile.CHOSENBOOKS = new System.Collections.Specialized.StringCollection( );    foreach (ListItem item in this.cblBooks.Items)    {       if (item.Selected)       {          Profile.CHOSENBOOKS.Add(item.Value.ToString( ));       }    }    Response.Redirect("Default.aspx"); }


Tip: Each time you save the books, you create an instance of the String collection, and then you iterate through the checked listboxes, looking for the selected items. Each selected item is added to the String collection within the profile (the CHOSENBOOKS property).

To confirm that this data has been stored, add a listbox (lbBooks) to the Default.aspx page, and bind that listbox to the collection in the profile:

public partial class Default_aspx : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {       if (Profile.UserName != null && Profile.IsAnonymous =  = false)       {          this.lblFullName.Text = "Full name: " +             Profile.firstName + " " + Profile.lastName;          this.lblPhone.Text = "Phone: " + Profile.phoneNumber;          this.lblBirthDate.Text = "Born: " +            Profile.birthDate.ToShortDateString( );          this.pnlInfo.Visible = true;       }       else       {          this.pnlInfo.Visible = false;       }            if (Profile.CHOSENBOOKS != null)       {          this.lbBooks.DataSource = Profile.CHOSENBOOKS;          this.lbBooks.DataBind( );          this.lbBooks.Visible = true;       }       else       {          this.lbBooks.Visible = false;       }     }

To make your code a bit easier to maintain, you want to have the selected values (name, phone, selected books, etc.) prefilled when you return to the Profile Information page, so you'll implement a bit of code on Page_Load to get the initial values from the Profile object:

public partial class ProfileInfo_aspx : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {       if (!IsPostBack && Profile.UserName != null)       {          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.cblBooks.Items)             {                foreach (string s in Profile.CHOSENBOOKS)                {                   if (li.Text =  = s)                   {                      li.Selected = true;                   }   // end if text is the same                }      // end foreach string in saved isbns             }         // end foreach item in the listbox          }            // end if savedisbns not null       }               // end if not postback     }            // end Page Load

Each time you navigate to the Profile Information page, the values are updated from the existing profile (if any) and you are free to change them and save the changes, as shown in Figure 4-34.

Figure 4-34. Profile information with "shopping cart"


When you return to the default page, your saved profile information is reflected, as shown in Figure 4-35.

Figure 4-35. Shopping-cart choices in profile


4.5.2. What about...

...anonymous personalization?

Most sites would like to allow the user to make choices (for example, add to a shopping cart) before logging in. In this lab that is not possible, but anonymous personalization is covered in the next lab.

4.5.3. Where can I learn more?

Numerous articles on personalization are available on ONDotnet (http://www.ondotnet.com), as well as in the MSDN.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net