Click here to Skip to main content
13,500,000 members (52,501 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
I have a UserControl called Ad that represents an advertisement. It uses attributes and elements to populate its properties:

<mabel:Ad runat="server" ID="Ad3" Weight="1">
    <Title>Fundraise with us!</Title>
    <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>

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

public partial class Ad : UserControl
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    public string Title { get; set; }
    /// <summary>
    /// Gets or sets the image.
    /// </summary>
    /// <value>The image.</value>
    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 17-May-11 3:50am
Updated 17-May-11 3:53am
SAKryukov 17-May-11 16:12pm
Not clear. Why "parsing" anything in a control which is totally yours?
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.
SAKryukov 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.
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.
SAKryukov 17-May-11 19:32pm
Understood, but is the parsing of the markup also on you? I guess only the mapping part is...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What's wrong with using built-in collections?

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 24 May 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100