Visual Basic.NET
  Home arrow Visual Basic.NET arrow A Deeper Look at Personalization using Vis...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

A Deeper Look at Personalization using Visual Basic 2005
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2006-11-30

    Table of Contents:
  • A Deeper Look at Personalization using Visual Basic 2005
  • Anonymous Personalization
  • Migrating the Anonymous Data to the Actual User’s Record
  • Themes and Skins

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Deeper Look at Personalization using Visual Basic 2005


    (Page 1 of 4 )

    In this third part of a four-part series covering personalization with Visual Basic 2005, we'll learn about anonymous personalization, themes and skins, and more. This article is excerpted from chapter 12 of the book Programming Visual Basic 2005, written by Jesse Liberty (O'Reilly, 2005; ISBN: 0596009496).

    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.


    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 theString 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 (theCHOSENBOOKSproperty).

    You also need to overridePage_Loadso that this page will open with the user’s profile information updated, as shown in Example 12-15.

    Example 12-15. Modified ProfileInfo.aspx.vb

    Partial 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 The
    n
              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 CheckBoxList

    To confirm that this data has been stored, add aListBox(name itlbBooks) to the pnlInfo panel you added to Welcome.aspx page, as shown in Figure 12-39.


    Figure 12-39.  ListBox added to panel

    Bind 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

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Visual Basic 2005," published...
     

    Buy this book now. This article is excerpted from the book Programming Visual Basic 2005, written by Jesse Liberty (O'Reilly, 2005; ISBN: 0596009496). Check it out today at your favorite bookstore. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek