Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET WebForms , +
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>
    <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:
 
[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 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 2:50am
Edited 17-May-11 2:53am
v2
Comments
SAKryukov at 17-May-11 16:12pm
   
Not clear. Why "parsing" anything in a control which is totally yours?
--SA
Yvan Rodrigues at 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 at 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 at 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 at 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

Rate this: bad
good
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
           Ctrl.Render()
       End If
    Next
End Sub
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 449
1 Maciej Los 274
2 CPallini 160
3 Sergey Alexandrovich Kryukov 152
4 Richard MacCutchan 130
0 OriginalGriff 5,474
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,434


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