Collection Editor Example
This example shows how to create a control named ContactCollectionEditor that implements a custom collection editor. The example shows how to specify the exact type of the object that a page developer can add to the control's collection property when using a custom collection editor. You associate a collection editor with a collection property (or the type of the property) by applying the EditorAttribute to the collection property of the control.
If you use a strongly typed IList implementation for your collection property, and all the objects in the collection are of the same type, you do not need a custom collection editor. In that case, you can rely on the built-in CollectionEditor as the property editor because CollectionEditor infers the object type from the type of the Items property of the IList implementation. You should use a typed collection whenever possible. However, if you use a collection such as ArrayList as the type of a controls' collection property, you need a custom collection editor to specify the collection items' object type.
The ContactCollectionEditor described in this example is used by the Contacts property of the QuickContacts control described in Web Control Collection Property Example. It enables objects of type Contact to be added to the Contacts property through a collection editor user interface (UI). The ContactCollectionEditor class derives fromCollectionEditor and overrides the CreateCollectionItemType method to return the Contact type.
If the collection property of your control contains objects of different types, implement a collection editor similar to the example but override the CreateNewItemTypes method instead of the CreateCollectionItemType method and return the correct item types.
Code Listing for the ContactCollectionEditor
' ContactCollectionEditor.vbImports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Reflection
Namespace Samples.AspNet.VB.Controls
PublicClass ContactCollectionEditor
Inherits CollectionEditor
PublicSubNew(ByVal newType As Type)
MyBase.new(newType)
EndSubProtectedOverridesFunction CanSelectMultipleInstances() _
AsBooleanReturnFalseEndFunctionProtectedOverridesFunction CreateCollectionItemType() As Type
ReturnGetType(Contact)
EndFunctionEndClassEndNamespace
// ContactCollectionEditor.csusing System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
namespace Samples.AspNet.CS.Controls
{
publicclass ContactCollectionEditor : CollectionEditor
{
public ContactCollectionEditor(Type type)
: base(type)
{
}
protectedoverridebool CanSelectMultipleInstances()
{
returnfalse;
}
protectedoverride Type CreateCollectionItemType()
{
returntypeof(Contact);
}
}
}
Building and Using the Example
Compile the ContactCollectionEditor editor with the QuickContacts control and the Contacts class listed in Web Control Collection Property Example. You must add a reference to the System.Design assembly for compilation.
For more information on compiling and using the custom control examples, see Building the Custom Server Control Examples.
See Also
Concepts
Web Control Collection Property Example
Building the Custom Server Control Examples