views:

137

answers:

1

I need to provide a way to allow users to select 1 Subject, but the subjects are grouped into Qualifications and so I want to show a heading for each 'group'. My first thought is an asp:RadioButtonList as that provides a list of options from which only one can be selected but there's no means to add break the list up with headings.

So I tried the following code - but I can't see that the LiteralControl is being added, which makes me think it's not a valid approach.

For Each item As Subject In Qualifications
        If item.QualCode <> previousQualCode Then
            rblSelection.Controls.Add(New LiteralControl(item.QualName))
        End If
        rblSelection.Items.Add(New ListItem(item.SubjectName, item.SelectionCode))
        previousQualCode = item.QualCode
    Next
A: 

No you can't do this. It is only valid to have ListItems. Another approach would be to have a RadioButton for each subject. Give them all the same GroupName which will mean you can only set one, but you can arrange them however you like on the page, under headings, maybe.

    <h3>Qualification 1</h3>
    <asp:RadioButton id="rbSubject" runat="server" />
    <asp:RadioButton id="rbSubject1" runat="server" />
    <asp:RadioButton id="rbSubject2" runat="server" />
    <h3>Qualification 2</h3>
    <asp:RadioButton id="rbSubject3" runat="server" />
    <asp:RadioButton id="rbSubject4" runat="server" />

Code Behind:

        rbSubject.GroupName = "grp1";
        rbSubject1.GroupName = "grp1";
        rbSubject2.GroupName = "grp1";
        rbSubject3.GroupName = "grp1";
        rbSubject4.GroupName = "grp1";

In your case, use a placeholdercontrol and add the dynamically generated controls and headers to the placeholders Controls collection.

For Each item As Subject In Qualifications
    If item.QualCode <> previousQualCode Then
        placeholder.Controls.Add(New LiteralControl(item.QualName))
    End If
    create new button here:- sorry Im a C# dev, I dont know the VB syntax and I am editing from my phone, but you should be able to work it out.

    placeholder.Controls.Add(btn)
    previousQualCode = item.QualCode
Next
Daniel Dyson
I didn't think it was possible, but I wasn't sure. The placeholder idea worked a treat thanks :)
Simon Martin
You are welcome. Could you please send me the completed code so that I can update my post in VB? [email protected]
Daniel Dyson