Click here to Skip to main content
15,888,175 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more: , +
I have a UserControl called Ad that represents an advertisement. It uses attributes and elements to populate its properties:

XML
<mabel:Ad runat="server" ID="Ad3" Weight="1">
    <Title>Fundraise with us!</Title>
    <Link>/info/fundraising+ideas</Link>
    <Image>fundraise.<ils:Query runat="server" />.jpg</Image>
    <Copy>Mabel's Labels' fundraisers are easy and profitable. Sign-up your school or organization.</Copy>
    <Action>Sign-Up Now</Action>
</mabel:Ad>


The setting of properties works well because I've used the appropriate attributes:

XML
[ParseChildren(true)]
public partial class Ad : UserControl
{
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    [Required]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Title { get; set; }
    /// <summary>
    /// Gets or sets the image.
    /// </summary>
    /// <value>The image.</value>
    [Required]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Image { get; set; }

...


My problem is that I'd like to parse server controls inside these child elements, such as the <ils:Query/> control shown in the <Image> element of the <mabel:ad xmlns:mabel="#unknown"> control.

Since I'm using the [ParseChildren(true)], these nested controls are not parsed because the elements are being parse as values to be assigned to properties.

The simplest solution I've come up with so far is applying a custom ControlBuilder to this server control, setting [ParseChildren(false)] and having the ControlBuilder set the values of each of the properties using reflection; after parsing the InnerText of each.

There must be a simple way .. ?
Posted
Updated 17-May-11 2:53am
v2
Comments
Sergey Alexandrovich Kryukov 17-May-11 16:12pm    
Not clear. Why "parsing" anything in a control which is totally yours?
--SA
Yvan Rodrigues 17-May-11 16:39pm    
Because I'd like child controls to render in a nested manner. For example in the markup above there is a <ils:Query runat="server" /> server control that expands to a language code (e.g. en, fr, es, etc.). I'd like to be able to do so without having to enumerate all child controls and call their RenderControl() methods. The framework is good at that, and I'd rather not reinvent the wheel.
Sergey Alexandrovich Kryukov 17-May-11 18:51pm    
This is not parsing. This is mapping data onto UI, recursively or not.
I don't see a problem here.
--SA
Yvan Rodrigues 17-May-11 19:09pm    
I guess... but only in the sense that all (most) controls map some sort of data (or markup anyways) onto the UI.

Parsing is the terminology that the framework uses. It invokes the default ControlBuilder which parses the markup and creates the controls represented by elements (with the runat="server" attribute). When it encounters the [ParseChildren(true)] attribute it does not attempt to parse child controls, because they are assumed to only contain values to be assigned to the control's properties. If [ParseChildren(false)] were applied, the parser would not stop, and would continue to parse child controls.
Sergey Alexandrovich Kryukov 17-May-11 19:32pm    
Understood, but is the parsing of the markup also on you? I guess only the mapping part is...
--SA

1 solution

What's wrong with using built-in collections?

VB
Sub CustomControl_PreRender(control As UserControl) Handles CustomControl.PreRender
    Dim ctrl as UserControl
    For Each ctrl in UserControl.Controls
       If Ctrl.Invalidated == True Then
           Ctrl.Render()
       End If
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900