Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 Collection Editor Example

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
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.

Visual Basic
' 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

C#
// 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);
        }
    }
}

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker