Filed under: Uncategorized
So you have a web part, but want it to be generic so that it can be reused in multiple places. So you do the standard thing add in some properties to your web part class and expose them via get and set, for example:
private string myString;
public string MyString
{
get { return myString; }
set { myString = value; }
}
However in order to enable this property to be useful in making a generic web part you need to decorate it with two properties:
- [WebBrowsable(true)]
- [Personalizable(PersonalizationScope.Shared)] – You can also set the personalization scope to User if you want users to be able to customize the web part themselves
Now you’ve got the functionality that you want, but you can take it further still. More than likely you will not be the only person using your web part. So have the property with the name “MyString” isn’t a very user friendly experience, most people will probably question why there is no space. In addition do you really want to rely on the name of the property to communicate exactly what it is used for? No, you probably want to have a description for the user as well. With that in mind you can also add the next two decorations:
- [WebDisplayName("My String")]
- [WebDescription("This is the description of my property.")]
Then one final thought, all the properties you define within your class will categorized as miscellaneous. Is that really where you want your property? What if you have several properties that aren’t directly related? In that case you might want to put them in different categories, for this you can use this decoration:
- [System.ComponentModel.Category("My Category")]