Question

My goal is to create a composite control that looks like, acts like and behaves like a RadioButtonList. There is some extra things done behind the scenes which are working no problem. What I am unable to accomplish is the desired markup to use the control. My ideal markup looks like this:

<cc1:RadioButtonField ID="rfCardType" runat="server" Title="Card Type:">
    <asp:ListItem Enabled="true" Text="MasterCard" />
    <asp:ListItem Enabled="true" Text="Visa" />
    <asp:ListItem Enabled="true" Text="Amex" />
</cc1:RadioButtonField>

What I would like to do is pass the <asp:ListItems> to the RadioButtonList in the composite control and have that handle everything required to produce/run the control.

Control Markup for RadioButtonField:

<div class="Title"> 
    <asp:Label ID="lblTitle" runat="server" AssociatedControlID="rblField" />
</div>
<div class="Text">
    <asp:RadioButtonList ID="rblField" runat="server" Visible="true">
    </asp:RadioButtonList>
</div>

Code Behind for RadioButtonField:

???

What does the RadioButtonField code behind need to do in order to collect the <asp:ListItems> and pass them to the RadioButtonList?

Was it helpful?

Solution

If you want the <ListItem> style markup, here's what you'll have to do:

  1. Add an items private field of type ListItemCollection to your composite control
  2. Add an Items public property of type ListItemCollection to your composite control. The getter should refer to the items private field.
  3. Add the ParseChildren class attribute to your composite control so it will read the list.

Your composite control now has the ability to read ListItem nodes from its markup. When the control renders, all <ListItem> nodes will be added to the private items collection.

It would be wonderful if you could now just set the Items member of the RadioButtonList, but unfortunately it is private. You will have to foreach through the items field and call the Add() method on your child RadioButtonList.

OTHER TIPS

[PersistenceMode(PersistenceMode.innerProperty)]
[DesignerSerializationVisibility(DesignerSerilizationVisibility.Content)]
ListCollection Items
{
}

Maybe try this two attributes. The PersistChildrenAttribute only provides designer support for your control with Visual Studio. DesignerSerializationVisibility.Content serializes the contents of the collection data type

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top