Control with Complex Types and Inner Controls

B

Bart Fibrich

Recently I needed to create a control that included both complex properties
and an inner control. Couldn't find much on the net so here it is.

[ParseChildren(true)]
public class OuterControl: WebControl
{
// A property that hold the inner control. (wrapped up in a property)
public InnerControl Control = new InnerControl();

public ComplexProp ComplexProperty = new ComplexProperty();

// Add the inner control to the outer control.
protected override void RenderContents(HtmlTextWriter output)
{
this.Controls.Add(_control);
base.RenderContents(output);
}
}

[ParseChildren(false)]
public class InnerControl: WebControl {}

public class ComplexProp
{
// Some complex fields
public int x;
public int y;
}

Usage :

<myPrefix:OuterControl runat="server">
<ComplexProperty x="1" y="2"/>
<Control>
<asp:Label Runat="server">hello world!</asp:Label>
</control>
</myPrefix:OuterControl>

If the complex property needs to be a collection of complex items then you need
to create a typed collection for the items and prefix the items.

<myPrefix:OuterControl runat="server">
<ComplexPropertyTypedCollection>
<myPrefix:ComplexProperty x="1" y="2"/>
<myPrefix:ComplexProperty x="3" y="4"/>
</ComplexPropertyTypedCollection>
<Control>
<asp:Label Runat="server">hello world!</asp:Label>
</control>
</myPrefix:OuterControl>
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top