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?

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

That does the trick. Now how would I achieve intellisense displaying the asp:ListItem as the available option?
Once the attribute is added and wired to the Items public property it should happen automatically.
It isn't happening automatically... hmmm
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...
Yes that's what I have, but it doesn't seem to help.
|
1
[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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.