Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Personalization (ASP.NET) - a new GotDotNet User Sample

UPDATE: Useful HttpCookie faq: http://www.cookiecentral.com/faq/ regarding limits on sizes of cookies etc.


I've uploaded a new GotDotNet User Sample titled: Personalization (ASP.NET). This is a wrapper around cookies to simplify the task of storing small amounts of personalization data. An example might be a webform that needs to remember all of the previous selections made on a per-user basis. This sample comes complete with a .chm file laced with stacks of sample usage code.

Personalization uses "Stores" to store data and each store can contain multiple keys. So, if I want to remember user settings for a page named "PageA.aspx" I could do:

Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
    
    ' create the store for this page
    Dim p As New Personalization("PageA.aspx")
    
    ' read in the values
    If Not Me.FieldA.Text.Trim = String.Empty Then
        p.SetValue("FieldA", Me.FieldA.Text.Trim)
    Else
        p.Remove("FieldA")
    End If
    
    If Not Me.FieldB.Text.Trim = String.Empty Then
        p.SetValue("FieldB", Me.FieldB.Text.Trim)
    Else
        p.Remove("FieldB")
    End If
End Sub
...then, reading the values in the next time the user visits is trivial:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    
        ' re-create the store
        Dim p As New Personalization("PageA.aspx")
        ' read in the items
        txtFieldA.Text = p.Items("FieldA")
        txtFieldB.Text = p.Items("FieldB")
    
    End If
    
End Sub

Get Personalization Here

1 Comment

Comments have been disabled for this content.