Parsing InnerProperties after modifying HTML (design time)

Discussion in 'ASP .Net Building Controls' started by Oleg Ogurok, Dec 29, 2003.

  1. Oleg Ogurok

    Oleg Ogurok Guest

    Hi all,

    In my custom control, I have a property derived from BaseCollection. At
    design time, the property shows as (Collection[...]) in the Properties
    window, so I can click [...] button and add items to it. The items get shown
    as inner XML tags. (An example of what I'm trying to do is DropDownList's
    Items property)

    The problem is when I switch to HTML (XML) mode and manually change one of
    the attributes of my control (or add another item), and then switch back to
    the Design mode, I get "Error Creating Control. "" could not be set on
    property Nodes."

    Something is telling me I have to somehow tell VS.NET to parse the inner XML
    back, create a brand new collection and then pass it to the control class
    for rendering. The code works fine at runtime but not at design time.


    Any ideas? Here's the code:

    [DefaultProperty("Nodes"),
    ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>"),
    ParseChildren(true), PersistChildren(false)]
    public class MyControl : System.Web.UI.WebControls.WebControl

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    NotifyParentProperty(true),
    PersistenceMode(PersistenceMode.InnerProperty)]
    public NodeCollection Nodes
    {
    get
    { return nodes;
    }
    set
    { nodes = value;
    }
    }


    The generated HTML (XML) is as follows

    <xxx:mycontrol id="mycontrol1" runat="server">
    <Nodes>
    <xxx:Node Title="Node1"></xxx:Node>
    <xxx:Node Title="Node2"></xxx:Node>
    </Nodes>
    </xxx:mycontrol>
     
    Oleg Ogurok, Dec 29, 2003
    #1
    1. Advertising

  2. Oleg Ogurok

    Teemu Keiski Guest

    Hi,

    put the property to be read-only and so that collection instance is created
    when collection is accessed for the first time.

    public NodeCollection Nodes
    {
    get
    {
    if(nodes==null)
    {
    nodes=new NodeCollection()
    }
    return nodes;
    }
    }

    This way control itself is responsible for creating the collection instance.
    If you keep the property as read/write, you need to write a custom
    typeconverter for it so that collection instance can be created properly.

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



    "Oleg Ogurok" <> wrote in
    message news:899e6$3fef9992$825ec943$-service.com...
    > Hi all,
    >
    > In my custom control, I have a property derived from BaseCollection. At
    > design time, the property shows as (Collection[...]) in the Properties
    > window, so I can click [...] button and add items to it. The items get

    shown
    > as inner XML tags. (An example of what I'm trying to do is DropDownList's
    > Items property)
    >
    > The problem is when I switch to HTML (XML) mode and manually change one of
    > the attributes of my control (or add another item), and then switch back

    to
    > the Design mode, I get "Error Creating Control. "" could not be set on
    > property Nodes."
    >
    > Something is telling me I have to somehow tell VS.NET to parse the inner

    XML
    > back, create a brand new collection and then pass it to the control class
    > for rendering. The code works fine at runtime but not at design time.
    >
    >
    > Any ideas? Here's the code:
    >
    > [DefaultProperty("Nodes"),
    > ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>"),
    > ParseChildren(true), PersistChildren(false)]
    > public class MyControl : System.Web.UI.WebControls.WebControl
    >
    >

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    > NotifyParentProperty(true),
    > PersistenceMode(PersistenceMode.InnerProperty)]
    > public NodeCollection Nodes
    > {
    > get
    > { return nodes;
    > }
    > set
    > { nodes = value;
    > }
    > }
    >
    >
    > The generated HTML (XML) is as follows
    >
    > <xxx:mycontrol id="mycontrol1" runat="server">
    > <Nodes>
    > <xxx:Node Title="Node1"></xxx:Node>
    > <xxx:Node Title="Node2"></xxx:Node>
    > </Nodes>
    > </xxx:mycontrol>
    >
    >
     
    Teemu Keiski, Dec 30, 2003
    #2
    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. Nomen Nescio
    Replies:
    2
    Views:
    2,069
    Sean M
    Jun 28, 2005
  2. =?Utf-8?B?YWRz?=
    Replies:
    2
    Views:
    564
    =?Utf-8?B?YWRz?=
    Feb 19, 2006
  3. PC Leung
    Replies:
    0
    Views:
    880
    PC Leung
    Jun 28, 2004
  4. flamesrock
    Replies:
    8
    Views:
    486
    Hendrik van Rooyen
    Nov 24, 2006
  5. Markus Fischer
    Replies:
    2
    Views:
    204
    Markus Fischer
    Apr 5, 2011
Loading...

Share This Page