Custom collection design time support

Discussion in 'ASP .Net Building Controls' started by David Keenan, Oct 9, 2003.

  1. David Keenan

    David Keenan Guest

    Hi Everyone,

    I'm having problems implementing design time support for a
    custom collection in a custom web control. I have managed
    to get it to display the collection editor and persist the
    collection items into the code of my code behind file.

    However, the properties box for the collection does not
    display (Collection) as it should, it is simply blank.
    Also, after having added items, when I go to edit the
    collection again it does not populate the collection
    editor with items I have already added, even though they
    have been written to the code. I'm sure I have probably
    missed something simple, possibly a missing Attribute on
    one of my classes??? Any help would be appreciated!

    Below is the code I am using, it should copy and paste OK
    into a new Web Control Library project. It contains my
    Custom control class (TestControl), my item class
    (MyClass), my custom collection class (MyClassCollection)
    and a custom converter for my item class
    (MyClassConverter).


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Collections;
    using System.Reflection;
    using System.Globalization;
    using System.Windows.Forms;
    using System.ComponentModel.Design.Serialization;

    namespace TestWebControlLibrary
    {
    [DefaultProperty("TheList")]
    [ToolboxData("<{0}:TestControl runat=server></
    {0}:TestControl>")]
    public class TestControl :
    System.Web.UI.WebControls.WebControl
    {
    private MyClassCollection theList = null;

    [DesignerSerializationVisibility
    (DesignerSerializationVisibility.Content)]
    public MyClassCollection TheList
    {
    get{return this.theList;}
    }

    protected override void Render
    (HtmlTextWriter output)
    {
    output.Write("Test");
    }
    }

    [Serializable()]
    [ToolboxItem(false)]
    [DesignTimeVisible(false)]
    [TypeConverter(typeof(MyClassConverter))]
    public class MyClass : Component
    {
    private string text;

    public string Text
    {
    get{return this.text;}
    set{this.text = value;}
    }

    public MyClass()
    {
    this.text = "";
    }

    public MyClass(string text)
    {
    this.text = text;
    }
    }

    public class MyClassCollection :
    System.Collections.CollectionBase
    {
    public MyClass this[int index]
    {
    get{return (MyClass)(List[index]);}
    set{List[index] = (MyClass)value;}
    }

    public int Add(MyClass myClass)
    {
    if(!Contains(myClass))
    return List.Add(myClass);
    return List.Count;
    }

    public bool Contains(MyClass myClass)
    {
    return List.Contains(myClass);
    }

    public int IndexOf(MyClass myClass)
    {
    return List.IndexOf(myClass);
    }

    public void Insert(int index, MyClass
    myClass)
    {
    List.Insert(index, myClass);
    }

    public void Remove(MyClass myClass)
    {
    List.Remove(myClass);
    }

    public void CopyTo(MyClass[] array, int
    index)
    {
    List.CopyTo(array, index);
    }
    }

    public class MyClassConverter :
    ExpandableObjectConverter
    {
    public override bool CanConvertTo
    (ITypeDescriptorContext context, Type destinationType)
    {
    if (destinationType == typeof
    (InstanceDescriptor))
    {
    return true;
    }
    return base.CanConvertTo(context,
    destinationType);
    }

    public override object ConvertTo
    (ITypeDescriptorContext context, CultureInfo culture,
    object value, Type destinationType)
    {
    if(destinationType == typeof
    (InstanceDescriptor))
    {
    ConstructorInfo ci = typeof
    (MyClass).GetConstructor(new Type[]{typeof(string)});
    MyClass item = (MyClass)
    value;
    return new
    InstanceDescriptor(ci, new object[]{item.Text}, true);
    }
    return base.ConvertTo(context,
    culture, value, destinationType);
    }
    }
    }


    I'm really stumped on this one and have spent a couple of
    days now trying to figure it out.

    Please HELP!

    Thanks,

    Dave
    David Keenan, Oct 9, 2003
    #1
    1. Advertising

  2. David Keenan

    Teemu Keiski Guest

    Hi,

    Specify the collection property as follows:

    [
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    PersistenceMode(PersistenceMode.InnerProperty),
    NotifyParentProperty(true)
    ]
    public MyClassCollection TheList
    {
    get{
    if(theList==null)
    {
    theList=new MyClassCollection();
    }
    return theList;
    }
    }


    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "David Keenan" <> wrote in message
    news:0de501c38e7b$35356840$...
    > Hi Everyone,
    >
    > I'm having problems implementing design time support for a
    > custom collection in a custom web control. I have managed
    > to get it to display the collection editor and persist the
    > collection items into the code of my code behind file.
    >
    > However, the properties box for the collection does not
    > display (Collection) as it should, it is simply blank.
    > Also, after having added items, when I go to edit the
    > collection again it does not populate the collection
    > editor with items I have already added, even though they
    > have been written to the code. I'm sure I have probably
    > missed something simple, possibly a missing Attribute on
    > one of my classes??? Any help would be appreciated!
    >
    > Below is the code I am using, it should copy and paste OK
    > into a new Web Control Library project. It contains my
    > Custom control class (TestControl), my item class
    > (MyClass), my custom collection class (MyClassCollection)
    > and a custom converter for my item class
    > (MyClassConverter).
    >
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Collections;
    > using System.Reflection;
    > using System.Globalization;
    > using System.Windows.Forms;
    > using System.ComponentModel.Design.Serialization;
    >
    > namespace TestWebControlLibrary
    > {
    > [DefaultProperty("TheList")]
    > [ToolboxData("<{0}:TestControl runat=server></
    > {0}:TestControl>")]
    > public class TestControl :
    > System.Web.UI.WebControls.WebControl
    > {
    > private MyClassCollection theList = null;
    >
    > [DesignerSerializationVisibility
    > (DesignerSerializationVisibility.Content)]
    > public MyClassCollection TheList
    > {
    > get{return this.theList;}
    > }
    >
    > protected override void Render
    > (HtmlTextWriter output)
    > {
    > output.Write("Test");
    > }
    > }
    >
    > [Serializable()]
    > [ToolboxItem(false)]
    > [DesignTimeVisible(false)]
    > [TypeConverter(typeof(MyClassConverter))]
    > public class MyClass : Component
    > {
    > private string text;
    >
    > public string Text
    > {
    > get{return this.text;}
    > set{this.text = value;}
    > }
    >
    > public MyClass()
    > {
    > this.text = "";
    > }
    >
    > public MyClass(string text)
    > {
    > this.text = text;
    > }
    > }
    >
    > public class MyClassCollection :
    > System.Collections.CollectionBase
    > {
    > public MyClass this[int index]
    > {
    > get{return (MyClass)(List[index]);}
    > set{List[index] = (MyClass)value;}
    > }
    >
    > public int Add(MyClass myClass)
    > {
    > if(!Contains(myClass))
    > return List.Add(myClass);
    > return List.Count;
    > }
    >
    > public bool Contains(MyClass myClass)
    > {
    > return List.Contains(myClass);
    > }
    >
    > public int IndexOf(MyClass myClass)
    > {
    > return List.IndexOf(myClass);
    > }
    >
    > public void Insert(int index, MyClass
    > myClass)
    > {
    > List.Insert(index, myClass);
    > }
    >
    > public void Remove(MyClass myClass)
    > {
    > List.Remove(myClass);
    > }
    >
    > public void CopyTo(MyClass[] array, int
    > index)
    > {
    > List.CopyTo(array, index);
    > }
    > }
    >
    > public class MyClassConverter :
    > ExpandableObjectConverter
    > {
    > public override bool CanConvertTo
    > (ITypeDescriptorContext context, Type destinationType)
    > {
    > if (destinationType == typeof
    > (InstanceDescriptor))
    > {
    > return true;
    > }
    > return base.CanConvertTo(context,
    > destinationType);
    > }
    >
    > public override object ConvertTo
    > (ITypeDescriptorContext context, CultureInfo culture,
    > object value, Type destinationType)
    > {
    > if(destinationType == typeof
    > (InstanceDescriptor))
    > {
    > ConstructorInfo ci = typeof
    > (MyClass).GetConstructor(new Type[]{typeof(string)});
    > MyClass item = (MyClass)
    > value;
    > return new
    > InstanceDescriptor(ci, new object[]{item.Text}, true);
    > }
    > return base.ConvertTo(context,
    > culture, value, destinationType);
    > }
    > }
    > }
    >
    >
    > I'm really stumped on this one and have spent a couple of
    > days now trying to figure it out.
    >
    > Please HELP!
    >
    > Thanks,
    >
    > Dave
    Teemu Keiski, Oct 11, 2003
    #2
    1. Advertising

  3. David Keenan

    David Keenan Guest

    Thank you very much, I was beginning to tear my hair out
    over that! I knew it would be something relatively simple
    but couldn't find it anywhere.

    Thanks again!

    Dave

    >-----Original Message-----
    >Hi,
    >
    >Specify the collection property as follows:
    >
    >[
    >DesignerSerializationVisibility

    (DesignerSerializationVisibility.Content),
    >PersistenceMode(PersistenceMode.InnerProperty),
    >NotifyParentProperty(true)
    >]
    >public MyClassCollection TheList
    >{
    >get{
    > if(theList==null)
    > {
    > theList=new MyClassCollection();
    > }
    > return theList;
    > }
    >}
    >
    >
    >--
    >Teemu Keiski
    >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    >ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >"David Keenan" <>

    wrote in message
    >news:0de501c38e7b$35356840$...
    >> Hi Everyone,
    >>
    >> I'm having problems implementing design time support

    for a
    >> custom collection in a custom web control. I have

    managed
    >> to get it to display the collection editor and persist

    the
    >> collection items into the code of my code behind file.
    >>
    >> However, the properties box for the collection does not
    >> display (Collection) as it should, it is simply blank.
    >> Also, after having added items, when I go to edit the
    >> collection again it does not populate the collection
    >> editor with items I have already added, even though they
    >> have been written to the code. I'm sure I have probably
    >> missed something simple, possibly a missing Attribute on
    >> one of my classes??? Any help would be appreciated!
    >>
    >> Below is the code I am using, it should copy and paste

    OK
    >> into a new Web Control Library project. It contains my
    >> Custom control class (TestControl), my item class
    >> (MyClass), my custom collection class

    (MyClassCollection)
    >> and a custom converter for my item class
    >> (MyClassConverter).
    >>
    >>
    >> using System;
    >> using System.Web.UI;
    >> using System.Web.UI.WebControls;
    >> using System.ComponentModel;
    >> using System.Collections;
    >> using System.Reflection;
    >> using System.Globalization;
    >> using System.Windows.Forms;
    >> using System.ComponentModel.Design.Serialization;
    >>
    >> namespace TestWebControlLibrary
    >> {
    >> [DefaultProperty("TheList")]
    >> [ToolboxData("<{0}:TestControl runat=server></
    >> {0}:TestControl>")]
    >> public class TestControl :
    >> System.Web.UI.WebControls.WebControl
    >> {
    >> private MyClassCollection theList = null;
    >>
    >> [DesignerSerializationVisibility
    >> (DesignerSerializationVisibility.Content)]
    >> public MyClassCollection TheList
    >> {
    >> get{return this.theList;}
    >> }
    >>
    >> protected override void Render
    >> (HtmlTextWriter output)
    >> {
    >> output.Write("Test");
    >> }
    >> }
    >>
    >> [Serializable()]
    >> [ToolboxItem(false)]
    >> [DesignTimeVisible(false)]
    >> [TypeConverter(typeof(MyClassConverter))]
    >> public class MyClass : Component
    >> {
    >> private string text;
    >>
    >> public string Text
    >> {
    >> get{return this.text;}
    >> set{this.text = value;}
    >> }
    >>
    >> public MyClass()
    >> {
    >> this.text = "";
    >> }
    >>
    >> public MyClass(string text)
    >> {
    >> this.text = text;
    >> }
    >> }
    >>
    >> public class MyClassCollection :
    >> System.Collections.CollectionBase
    >> {
    >> public MyClass this[int index]
    >> {
    >> get{return (MyClass)(List[index]);}
    >> set{List[index] = (MyClass)value;}
    >> }
    >>
    >> public int Add(MyClass myClass)
    >> {
    >> if(!Contains(myClass))
    >> return List.Add(myClass);
    >> return List.Count;
    >> }
    >>
    >> public bool Contains(MyClass myClass)
    >> {
    >> return List.Contains(myClass);
    >> }
    >>
    >> public int IndexOf(MyClass myClass)
    >> {
    >> return List.IndexOf(myClass);
    >> }
    >>
    >> public void Insert(int index, MyClass
    >> myClass)
    >> {
    >> List.Insert(index, myClass);
    >> }
    >>
    >> public void Remove(MyClass myClass)
    >> {
    >> List.Remove(myClass);
    >> }
    >>
    >> public void CopyTo(MyClass[] array, int
    >> index)
    >> {
    >> List.CopyTo(array, index);
    >> }
    >> }
    >>
    >> public class MyClassConverter :
    >> ExpandableObjectConverter
    >> {
    >> public override bool CanConvertTo
    >> (ITypeDescriptorContext context, Type destinationType)
    >> {
    >> if (destinationType == typeof
    >> (InstanceDescriptor))
    >> {
    >> return true;
    >> }
    >> return base.CanConvertTo(context,
    >> destinationType);
    >> }
    >>
    >> public override object ConvertTo
    >> (ITypeDescriptorContext context, CultureInfo culture,
    >> object value, Type destinationType)
    >> {
    >> if(destinationType == typeof
    >> (InstanceDescriptor))
    >> {
    >> ConstructorInfo ci = typeof
    >> (MyClass).GetConstructor(new Type[]{typeof(string)});
    >> MyClass item = (MyClass)
    >> value;
    >> return new
    >> InstanceDescriptor(ci, new object[]{item.Text}, true);
    >> }
    >> return base.ConvertTo(context,
    >> culture, value, destinationType);
    >> }
    >> }
    >> }
    >>
    >>
    >> I'm really stumped on this one and have spent a couple

    of
    >> days now trying to figure it out.
    >>
    >> Please HELP!
    >>
    >> Thanks,
    >>
    >> Dave

    >
    >
    >.
    >
    David Keenan, Oct 13, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. onyxring
    Replies:
    0
    Views:
    494
    onyxring
    Feb 15, 2006
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    966
    Øyvind Isaksen
    May 18, 2007
  3. Replies:
    1
    Views:
    345
    justncase80
    Sep 27, 2005
  4. Patrick

    Custom asp.net control using collection - Design Time Refresh

    Patrick, Oct 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    366
    Patrick
    Oct 13, 2005
  5. MattBell
    Replies:
    2
    Views:
    221
Loading...

Share This Page