一.Enabling Personalization for Control Properties
❑ WebBrowsable: To make a property available to be changed when the user is customizing the
page in the browser, you add the WebBrowsable attribute to the property and pass the attribute
a value of True.
❑ Personalizable: To cause the customizations to be remembered, you add the Personalizable
attribute to a property and pass it a value of True. The Personalizable attribute also allows you
to control whether customizations are remembered only for the user making the change or are
applied for all users.
1.1Turning on Customization:
[System.Web.UI.WebControls.WebParts.Personalizable(true),
System.Web.UI.WebControls.WebParts.WebBrowsable(true)]
public string BookTitle
{
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Chapter5CustomControlsCS
{
[ToolboxData("<{0}:ThirdSample runat=server></{0}:ThirdSample>")]
public class ThirdSample : System.Web.UI.WebControls.WebParts.WebPart
{
string _BookTitle;
protected override void CreateChildControls()
{
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label lblBook = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Table tbl = new System.Web.UI.WebControls.Table();
System.Web.UI.WebControls.TableCell[] tc = new System.Web.UI.WebControls.TableCell[2];
System.Web.UI.WebControls.TableRow[] tr = new System.Web.UI.WebControls.TableRow[2];
tc[0] = new System.Web.UI.WebControls.TableCell();
tc[1] = new System.Web.UI.WebControls.TableCell();
tr[0] = new System.Web.UI.WebControls.TableRow();
tr[1] = new System.Web.UI.WebControls.TableRow();
tr[0].Cells.Add(tc[0]);
tr[1].Cells.Add(tc[1]);
tbl.Rows.Add(tr[0]);
tbl.Rows.Add(tr[1]);
lbl.Text = "Title:";
lblBook.Text = _BookTitle;
lblBook.Font.Bold = true;
lblBook.BorderColor = System.Drawing.Color.Black;
lblBook.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
lblBook.BorderWidth = 1;
tc[0].Controls.Add(lbl);
tc[1].Controls.Add(lblBook);
this.Controls.Add(tbl);
}
[System.Web.UI.WebControls.WebParts.WebBrowsable(true)]
public string BookTitle
{
get
{
return _BookTitle;
}
set
{
_BookTitle = value;
}
}
public override System.Web.UI.WebControls.WebParts.WebPartVerbCollection Verbs
{
get
{
System.Web.UI.WebControls.WebParts.WebPartVerb vrbEnglish =
new System.Web.UI.WebControls.WebParts.WebPartVerb("vrbEnglish", this.SetEnglish);
System.Web.UI.WebControls.WebParts.WebPartVerb vrbFrench =
new System.Web.UI.WebControls.WebParts.WebPartVerb("vrbFrench", this.SetFrench);
vrbEnglish.Text = "English";
vrbFrench.Text = "French";
System.Web.UI.WebControls.WebParts.WebPartVerb[] vrbsLanguage = new System.Web.UI.WebControls.WebParts.WebPartVerb[2];
vrbsLanguage[0] = vrbFrench;
vrbsLanguage[1] = vrbEnglish;
System.Web.UI.WebControls.WebParts.WebPartVerbCollection vrbs;
vrbs = new System.Web.UI.WebControls.WebParts.WebPartVerbCollection(vrbsLanguage);
vrbEnglish.Checked = true;
vrbEnglish.ImageUrl = "Book.Gif";
vrbEnglish.Enabled = false;
return vrbs;
}
}
}
}
1,2Sharing Customizations
If you turn on personalization with the Personalizable attribute by passing a single parameter, the changes
made by a user will be applied to the page only when the page is requested by that user. If, instead of
passing True, you pass one of the Personalizable constants, you can not only turn on personalization, but
cause changes made to the page to be applied for all users:
❑ Webcontrols.WebParts.PersonalizationScope.User: Customized values for the property are
applied only to the current user (this is the default).
❑ Webcontrols.WebParts.PersonalizationScope.Shared: Customized values for the property are
applied to all users.
[[System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.Web.UI.WebControls.WebParts.WebBrowsable(true)]
public string BookTitle
{
}
1.3Enabling Customization for Inherited Properties
[System.Web.UI.WebControls.WebParts.Personalizable(true),
System.Web.UI.WebControls.WebParts.WebBrowsable(true)]
public override string AccessKey
{
get
{
return base.AccessKey;
}
set
{
base.AccessKey = value;
}
}
1.4Documenting Properties
Two other attributes can be applied to a customizable property:
❑ System.Web.UI.WebControls.WebParts.WebDescription: Accepts a single string parameter
that can be used to document the property.
❑ System.Web.UI.WebControls.WebParts.WebDisplayName: Accepts a single string parameter.
This name is used in the UI of the Web Part editors to provide a user-friendly name when users
are customizing a control in the browser.
1.5Accessing Attributes
At some point, you may want to determine from your code what the attribute settings are for one of the
subroutines or a function you’ve written as part of your control. To determine the value for an attribute
you must:
❑ Retrieve the collection of attributes for the class, property, method, or event.
❑ Find the attribute that you want.
❑ Examine the properties of the attributes.
System.ComponentModel.AttributeCollection atts;
ToolboxDataAttribute ts;
string strData;
atts = TypeDescriptor.GetAttributes(this);
ts = atts(GetType(ToolboxDataAttribute));
strData = ts.Data;
PropertyDescriptorCollection pd;
pd = TypeDescriptor.GetProperties(ts);
atts = pd(“Text”).Attributes;
In C#:
System.ComponentModel.AttributeCollection atts;
System.Web.UI.WebControls.WebParts.PersonalizableAttribute ps;
atts = TypeDescriptor.GetAttributes(this);
ps = atts(GetType(System.Web.UI.WebControls.WebParts.PersonalizableAttribute));
if(ps.Scope == System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)
{
...warn the user that customizations will be shared...
}
1.6Sharing Routines
System.Web.UI.WebControls.WebParts.WebPartEventHandler wevLang =
new System.Web.UI.WebControls.WebParts.WebPartEventHandler(this.SetLanguage);
System.Web.UI.WebControls.WebParts.WebPartVerb vrbEnglish =
new System.Web.UI.WebControls.WebParts.WebPartVerb(“LanguageMgr”, wevLang);