Dela via


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.vb
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Reflection

Namespace Samples.AspNet.VB.Controls
    Public Class ContactCollectionEditor
        Inherits CollectionEditor

        Public Sub New(ByVal newType As Type)
            MyBase.new(newType)
        End Sub

        Protected Overrides Function CanSelectMultipleInstances() _
        As Boolean
            Return False
        End Function

        Protected Overrides Function CreateCollectionItemType() As Type
            Return GetType(Contact)
        End Function
    End Class
End Namespace
// ContactCollectionEditor.cs
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;

namespace Samples.AspNet.CS.Controls
{
    public class ContactCollectionEditor : CollectionEditor
    {
        public ContactCollectionEditor(Type type)
            : base(type)
        {
        }

        protected override bool CanSelectMultipleInstances()
        {
            return false;
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof(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

Other Resources

Developing Custom ASP.NET Server Controls