A Deeper Look at Personalization using Visual Basic 2005 - Anonymous Personalization (Page 2 of 4 )
It is common to allow your users to personalize your site before identifying themselves. A classic example of this is Amazon.com, which lets you add books to your shopping cart before you log in (you only need to log in when you are ready to purchase what is in your cart).
ASP.NET 2.0 supports personalization for anonymous users as well as the ability later to link anonymous personalized data with a specific user’s. Once that user logs in, you don’t want to lose what was in the user’s cart.
To enable anonymous personalization, you must update your Web.config file adding:
<anonymousIdentification enabled="true" />
Add the attribute-value pair allowAnonymous="true" to the CHOSENBOOKS element of Web.config, as shown in Example 12-17.
Example 12-17. Modified Web.config for anonymous access
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/
.NetConfiguration/v2.0">
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="data source=.\SqlExpress;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" />
<roleManager enabled="true" />
<authentication mode="Forms"/>
<membership defaultProvider="AspNetSqlMembershipProvider"/>
<compilation debug="true"/>
<profile enabled="True" defaultProvider="AspNetSqlProfileProvider">
<properties>
<add name="lastName" />
<add name="firstName" />
<add name="phoneNumber" />
<add name="birthDate" type="System.DateTime"/>
<add name="CHOSENBOOKS" allowAnonymous="true"
type="System.Collections. Specialized.StringCollection" />
</properties>
</profile>
</system.web>
</configuration>
Redesign your Welcome.aspx page in two ways: first move the hyperlink to the profile Information page outside of the Logged In template. Second move the listbox (lbBooks) outside the panel. Thus, you can see both of these features whether or not you are logged in. Also, change the text on the Add Profile Info hyperlink to just Profile Info, since you will be using this link to add and edit the profile info.
When an anonymous user fills in the profile information, the user will automatically be assigned a Globally Unique Identifier (GUID), and an entry will be made in the database for that ID. However, note that only those properties marked withallowAnonymous may be stored, so you must modify yourSave_Clickevent handler in ProfileInfo.aspx.vb. Bracket the entries for all the profile elements except CHOSENBOOKSin anIfstatement that tests whether the user is currentlyAnonymous. The newsave_Clickevent handler for ProfileInfo.aspx.vb is shown in Example 12-18.
Example 12-18. Modified Save_Click event handler for ProfileInfo.aspx.vb
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)
End If
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
Response.Redirect("Welcome.aspx")
End Sub
The effect of the new code shown in Example 12-18 is that you check whether theIsAnonymousproperty is false. If it is, then you are dealing with a logged-in user, and you can get all of the properties; otherwise, you can get only those that are allowed for anonymous users.
Modify the ProfileInfo page so that the non-anonymous data is in a panel that will be invisible for users who are not logged in. The simplest way to do this may be to switch to Source view and bracket the nonanonymous code inside a panel (don’t forget to end the table before ending the panel), as shown in Example 12-19.
Example 12-19. Adding a nonanonymous information panel to ProfileInfo.aspx.vb
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlNonAnonymousInfo" runat="server">
<table>
<tr>
<td>First Name: </td>
<td style="width: 193px">
<asp:TextBox ID="FirstName" Runat="server" />
</td>
</tr>
<tr>
<td>Last Name: </td>
<td style="width: 193px">
<asp:TextBox ID="LastName" Runat="server" /></td>
</tr>
<tr>
<td>Phone number: </td>
<td style="width: 193px">
<asp:TextBox ID="Phone" Runat="server" />
</td>
</tr>
<tr>
<td>BirthDate</td>
<td style="width: 193px">
<asp:TextBox ID="BirthDate" Runat="server" />
</td>
</tr>
</table>
</asp:Panel>
Modify the Page_Load for ProfileInfo.aspx to hide the panel if the user is anonymous, as shown in Example 12-20.
Example 12-20. Modified page load—ProfileInfo.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
If Profile.IsAnonymous = True Then
Me.pnlNonAnonymousInfo.Visible = False
Else
Me.pnlNonAnonymousInfo.Visible = True
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 'Profile.CHOSENBOOKS IsNot Nothing
End If 'Profile.IsAnonymous = True
End If 'Not IsPostBack And Profile.UserName IsNot Nothing
End Sub
Run the application. Do not log in, but do click the Profile Info link. Select a few books and click Save. When you return to the Welcome page, you are still not logged in, but your selected books are displayed, as shown in Figure 12-41.
Stop the application and reopen the database. You’ll see that an ID has been created for this anonymous user (and theUserNamehas been set to the GUID generated). In addition, the profile information has been stored in the corresponding record, as shown in Figure 12-42.

Figure 12-41. Anonymous user information

Figure 12-42. Anonymous user record in database
Next: Migrating the Anonymous Data to the Actual User’s Record >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
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.
|
|