Q: In one of the articles that you wrote, you used the ParseChildren(false) and the PersistChildren(false) attributes on your web control class. What are they for? And why are the necessary?
A: Looking at the Microsoft documentation, I suppose you were confused on what these attributes are. I don’t blame you because the documentation assumes that you have quite a bit of experience. Essentially, these two attributes bitwise ‘switches’ which describes to ASP.NET on how to parse your control’s contents – the “contents” are what is found between the tags of your server control on your *.aspx or *.ascx files. Take for example the following code, of which, the contents are underlined.
<asp:Panel runat="server" ID="pnlSomePanel">
<asp:Button runat="server" OnClick="SomeButton_Click" Text="Click Me"/>
</asp:Panel>
To simply put the above code into explanation of the ASP.NET parser does: the inner content is parsed as controls and is added to the panel’s control collection. You realize this because you probably do the above almost everyday. However, take the prior statement about what the ASP.NET parser does and break it down into actions. These actions are, in generic terms:
- Treat the inner content as …
- Persist the inner content to …
And these two actions, my friend, are what those two attributes are for describing the actions. To iterate through that list again but describing them to the attribute:
- ParseChildrenAttribute
- PersistChildrenAttribute
The argument in the ParseChildrenAttribute is there to describe whether or not the contents are treated as child properties or not (“true” for child properties and “false” for parsing the contents as server controls). The argument of the PersistChildrenAttribute is there to describe to the design time environment (for example, Visual Studio.NET) on how to persist the changes through the IDE – whether or not to persist it as child controls (“true” means they are persisted as server controls and “false” is not).