views:

676

answers:

1

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?

+2  A: 

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.

Dave Swersky
That does the trick. Now how would I achieve intellisense displaying the asp:ListItem as the available option?
Gavin Miller
Once the attribute is added and wired to the Items public property it should happen automatically.
Dave Swersky
It isn't happening automatically... hmmm
Gavin Miller
Have you looked up the usage of the ParseChildren attribute? I think it should be "ParseChildren(true, "Items")" where "Items" is the public ListCollection property...
Dave Swersky
Yes that's what I have, but it doesn't seem to help.
Gavin Miller
Found it- you also need the PersistChildren attribute set to false. http://www.west-wind.com/WebLog/posts/200.aspx
Dave Swersky