MarkItUp.Web.Personalization
Categories
Just wanted to discuss a component that I've uploaded to ProjectDistributor:
Personalization
This is one of the first web controls that I created for myself back when I started getting into ASP.NET and is designed to abstract away the complexity of working with cookies which have many keys and sub-keys. The component itself is designed to work around the notion of a "store" - which is the abstraction of a key. When you work with Personalization you must mention which store you are working with up front, ie:
Personalization p = new Personalization("CommentForm_ascx") ; if
( p.Items["chkRememberMe"] != null ) {
this.chkRememberMe.Checked = bool.Parse(p.Items["chkRememberMe"].ToString()) ;
if( this.chkRememberMe.Checked ) {
this.txtName.Text = p.Items["txtName"].ToString() ;
this.txtUrl.Text = p.Items["txtUrl"].ToString() ;
}
} The above snippet opens a store named "CommentForm_ascx" and reads keys (which map to sub-keys) from it. Notice how the component lends itself to logically creating stores around specific page functions. This snippet would normally appear in your page load code to set the initial state of a page based on some persisted personalization settings.
Conversely, in your page unload (or pre render) logic, you should persist settings back into the personalization store like so:
protected
override void OnPreRender(EventArgs e) {
base.OnPreRender (e);
Personalization p = new Personalization("CommentForm_ascx") ;
if( this.chkRememberMe.Checked ) {
p.SetValue("chkRememberMe", "true");
p.SetValue("txtName", this.txtName.Text);
p.SetValue("txtUrl", this.txtUrl.Text);
}else{
p.Remove("chkRememberMe") ;
p.Remove("txtName") ;
p.Remove("txtUrl") ;
}
}