views:

24

answers:

3

Hello,

I want to generate this:

<x:MyControl id="a" runat="server">
   <Scripts>
        <x:MyScript .. />
   </Scripts>
</x:MyControl>

I setup a control like:

[
ParseChildren(true, "Scripts"),
DefaultProperty("Scripts")
]
public class MyControl: Control
{

    [
    PersistenceMode(PersistenceMode.InnerDefaultProperty),
    MergableProperty(false)
    ]
    public MyScriptCollection Scripts
    {
        get { return _scripts; }
        set { _scripts = value; }
    }
}

But this isn't working... It won't let me create a list of scripts... what is wrong with my definition?

EDIT: Also, MyScript objects in the MyScriptCollection does not inherit from the control base class.

Thanks.

A: 

I've just written these test classes:

[ParseChildren(true)]
public class MyControl : Control
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public MySubControlCollection SubControls { get; set; }

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        foreach (var control in SubControls)
        {
            writer.Write(control.Name);
        }
    }
}

public class MySubControlCollection : Collection<MySubControl>
{
}

public class MySubControl : Control
{
    public string Name { get; set; }
}

And this test markup:

<test:MyControl runat="server">
    <SubControls>
        <test:MySubControl runat="server" Name="Hello" />
        <test:MySubControl runat="server" Name="World" />
    </SubControls>
</test:MyControl>

And it works as expected. Can you describe what you mean by "isn't working"?

Rex M
It gives me the <SubControls /> element, but doesn't let me enter any children.... I'll give this setup a shot. Thanks.
Brian
Oh, I don't want the sub-items to be controls... I want them to be objects... similar to the ListControl base class or the GridView with its columns. How would you change it for that?
Brian
A: 

When you say "It won't let you create..." do you mean that intellisense doesn't populate things for you in the html editor? Have you tried creating the elements anyways and see if it works?

You have to define your custom html elements and attributes for the control to have them show up in intellisense, so you have to just ignore it until you have things the way you want them.

Mystere Man
It gives me the <Scripts/> element, but doesn't let me enter any children....
Brian
A: 

It seems like there is an issue if children aren't custom controls at times, especially with more complex markup in controls and longer inheritance trees. Not always the case, but for me it seems consistent.

Maybe this is a bug, maybe not.

Brian