all groups > asp.net building controls > february 2007 >
You're in the

asp.net building controls

group:

Persisting Child Controls, Property Collections and Individual Properties


Persisting Child Controls, Property Collections and Individual Properties Mark Olbert
2/21/2007 12:09:32 PM
asp.net building controls:
I have a custom composite control whose markup needs to contain child controls, an array property, and simple properties, all as
nested tag elements, e.g.:

<cc1:MyComposite runat="server" id="whatever" SimpleProp1="somevalue">
<ChildControls> <-- this is a property collection, of controls
<ChildControl1 .../>
<ChildControl2 ...>
<Fields> <-- this is actually a property collection, not a control
<Field... />
<Field... />
</Fields>
</ChildControl2>
</ChildControls>
<CustomStyle1 /> <-- these are "simple" style properties
<CustomStyle2 />
</cc1:MyComposite>

I'm confused as to what's the best way to proceed with parsing and persisting this kind of structure. I can get it to work by
treating each of the subitems as custom controls, setting them to ParseChildren(false)/PersistChildren(true), creating custom
control builders and then overriding AppendParsedSubObject() to add the things that are actually just property collections to the
appropriate property collection.

But I'd like to know if there's a better way that relies more on the existing plumbing in ASP.NET2. Particlarly because persisting
the style properties would seem to be a bit of a bear (i.e., I haven't been able to find any kind of analog to ControlPersister for
styles.

RE: Persisting Child Controls, Property Collections and Individual Properties stcheng NO[at]SPAM online.microsoft.com
2/22/2007 12:00:00 AM
Hello Mark,

As for custom composite control which contains child controls, so far I
only the following common patterns (according to the built-in ASP.NET web
server controls and the supported custom webcontrol model):

1. Do not directly embed child control type into innet properties, but use
some helper classes .e.g.

<cc1:MyComposite runat="server".........>
<subitems>
<item></item>
<item></item>
..........
</subitems>
...........
</cc1:MyComposite>

Thus, you need to set ParseChildren to true here so that those inner markup
are correctly populated as your propeties(collection) and at runtime
code(CreateChildcontrols), you construct the actual child controls
according to these helper class objects collection.


2. Directly put sub controls into your custom control's inner markup,
however, you should use template based control or GridView like control.
For exmple, you define those child controls like the Columns in GridView.
You can provide some built-in column types which has fixed control
structure at runtime, or you can provide a templatecolumn so that user can
put customize template. At runtime in CreateChildControls

3. Put "ParseChildren" as true and manually use control builder to parse
sub controls and or types.


IMO, I would proper the #1 and #2 since it will be more straighforward and
make the child control creation more clearly(in our CreateChildcontrols
code).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.


AddThis Social Bookmark Button