Attention: We are retiring the ASP.NET Community Blogs. Learn more >

ContextMenu - more planning and a proof of concept

One of the requirements for my ContextMenu control is to have a declarative syntax like this:

<ctl:ContextMenu attribs{...}>
    <ctl:ContextMenuItem attribs{...} />
    <ctl:ContextMenuItem attribs{...} />
    <ctl:ContextMenuItem attribs{...} />
</ctl:ContextMenu>

 

So the issue here is, how do I get the ContextMenuItems to be parsed as separate objects and added to my ContextMenu's Items collection property at runtime? The first thing to do is to specify the ParseChildren attribute on the ContextMenu class to instruct the Page parser that it has nested content which needs to be parsed as separate controls.

Using ParseChildren attribute.

This attribute can be applied at Class level and is used to instruct the Page parser how to interpret any nested elements that it encounters when parsing that control. The following page has some useful information about using this attribute:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusingparsechildrenattribute.asp?frame=true

After reading through the the chart it seemed apparent that I needed to apply the ParseChildrenAttribute( True, "Items" ) attribute to my ContextMenu class. As a proof-of-concept, I compiled the example from following page:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiparsechildrenattributeclasstopic.asp?frame=true

Sure enough, a quick compile of that code and attempting to use it with the following declarative syntax:

<ctl:CollectionPropertyControl header="test">
    <ctl:Employee name="Foo" title="Mr" alias="Foo" />
    <ctl:Employee name="Bar" title="Dr" alias="Bar" />
    <ctl:Employee name="Foobar" title="Prof" alias="Foobar" />
</ctl:CollectionPropertyControl>

 

... results in:

Collection Property control rendered

Collection Property - Quick Watch at runtime

... Perfect!


Further reading
Control Parsing, ParseChildrenAttribute, and Control Builders

No Comments