I've been trying to create a special kind of fieldset. Which does all kind of fantastic things, but mainly collapse and maintain state. Also the two parts of the fieldset (in the legend, and in the rest) must be available to code behind (declaratively).
The code in the consuming page or control should look something like this:
<myTagPrefix:Fieldset>
<myTagPrefix:Legend>[controls here should be available in codebehind]</myTagPrefix:Legend>
<myTagPrefix:Content>[controls here should be available in codebehind]</myTagPrefix:Content>
</myTagPrefix:Fieldset>
Or
<myTagPrefix:Fieldset>
<Legend>[controls here should be available in codebehind]</Legend>
<Content>[controls here should be available in codebehind]</Content>
</myTagPrefix:Fieldset>
Which would produce more-or-less the following HTML (excluding the magic collapsing and state-maintaining code):
<fieldset>
<legend>[result of rendered legend controls]</legend>
[result of rendered legend controls]
</fieldset>
I've looked into a templated control, exposing template container via properties marked as 'TemplateContainer', which works nice, except for the fact that the code behind cannot access the controls in the template anymore.
I also looked into inheriting a container control like panel, and override the render methods for the begin- and end-tag, which is also nice, except for that it can contain only one control collection, while this fieldset control should have two (the controls in the legend and the controls in the rest fieldset). This could be overcome by exposing the text of the legend as a property of the fieldset, but to keep things complicated, text is not the only thing to be displayed in the legend. (for instance: images and buttons can be displayed too).
This question can be seen more abstractly of course, I'm basically asking for a container control with multiple child control collections.
When I tried to figure out what to compare this with, the functionality comes close to a MultiView; a MultiView can only contain controls of Type 'View', and the controls of a View are available in code-behind. The fact that a MultiView does not restrict the number and uniqueness of its childcontrols, and my control should (a maximum of one legend, and one content element) is something I could live with ... for now ;)
Does anyone have an idea how the MultiView was built? Is there a trick I'm overlooking? Any help or suggestion would be appreciated.