Collection Property is often used when you create a server control. I will summarize some of the learned knowledge points in my development, which may be helpful to beginners.
If you have developed controls, you must know that if the properties of your controls are basic class data types, you do not need to save and restore these properties ,. net has completed these support work. however, if you want to add a set property to the control, it's not that easy! Set attributes are very common, such as ListItemCollection in ListBox and GridViewColumn in GridView.
Below, I will list some key points in development:
1. Set the following properties for the Control Level Control Class Level:
[ParseChildren (true)]
Public class MyControl: WebControl
2. Property Level settings are as follows:
[DesignerSerializationVisibility (DesignerSerializationVisibility. Content)]
[PersistenceMode (PersistenceMode. InnerProperty)]
[Editor (typeof (MyCollectionEditor), typeof (System. Drawing. Design. UITypeEditor)]
Public MyCollection Items
{
Get {}
}
Note: All set attributes should be read-only!
3. Implement MyCollectionEditor.
MyCollectionEditor
Class MyCollectionEditor: CollectionEditor
{
Public MyCollectionEditor (Type type): base (type)
{
}
Protected override bool CanSelectMultipleInstances ()
{
Return false;
}
Protected override System. Type CreateCollectionItemType ()
{
Return typeof (MyItem );
}
Protected override string GetDisplayText (object value)
{
MyItem exp = value as MyItem
// Return exp. ToString ();
Return exp. DisplayText;
}
}
As you can see, it is not difficult to implement this. The basic class CollectionEditor in the Framework already provides us with the 99% feature. We just need to customize it! Note that the first three processes are indispensable. The last one is to display the project in the edit dialog box. It can be a random string.