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