Parsing InnerProperties after modifying HTML (design time)

O

Oleg Ogurok

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>
 
T

Teemu Keiski

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top