Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add items between RadioButtonList?

Tags:

asp.net

We have a list that is getting generated server side, then using <asp:RadioButtonList... to display it.

How ever, how would we add some HTML after one of the items?

The idea is that if one of them is generated, there are some 'sub' radio buttons that get generated.

Thanks

ASP.NET:

private void PopulateFollowUpRadioButtons(Order order)
        {
            //Based on the options selected while creating the order template populate the radio button list.
            if (order.OrderTemplate.fur01Visible)
                FolloupRadioButtonList.Items.Add(new ListItem(order.OrderTemplate.fur01Label, "1"));
            if (order.OrderTemplate.fur02Visible)
                FolloupRadioButtonList.Items.Add(new ListItem(order.OrderTemplate.fur02Label, "2"));
            if (order.OrderTemplate.fur03Visible)
                FolloupRadioButtonList.Items.Add(new ListItem(order.OrderTemplate.fur03Label, "3"));


            //If follow up option is already selected then select the option in the Follow Up Radio button control 
            if (order.FollowUpRoleId != null)
                FolloupRadioButtonList.SelectedValue = order.FollowUpRoleId.Value.ToString();
        }


<asp:RadioButtonList ID="FolloupRadioButtonList" CssClass="black" ForeColor="Black"  runat="server"></asp:RadioButtonList>
like image 364
MRC Avatar asked Jan 16 '26 18:01

MRC


2 Answers

Create your own custom radiobuttonlistcontrol:

Namespace Controls
    Public Class MyRadioButtonList
        Inherits RadioButtonList

        Protected Overrides Sub RenderItem(itemType As System.Web.UI.WebControls.ListItemType, repeatIndex As Integer, repeatInfo As System.Web.UI.WebControls.RepeatInfo, writer As System.Web.UI.HtmlTextWriter)
            writer.Write("<div>Extra content</div>")
            MyBase.RenderItem(itemType, repeatIndex, repeatInfo, writer)
        End Sub
    End Class

End Namespace

And use it as so.

First register the control to a page, or a user control:

<%@ Register Assembly="(projectnamespace)" Namespace="(projectnamespace).Controls" TagPrefix="cc1" %>

And then use the control:

<cc1:MyRadioButtonList ID="rdolist" runat="server" />

It's a basic radiobuttonlistcontrol in the server side, so just use it as you do now.

like image 92
Esko Avatar answered Jan 19 '26 18:01

Esko


Technically you can assign your HTML directly to ListItem text e.g. this code

 ListItem li = new ListItem("aaaaa", "aaaaa");
 FolloupRadioButtonList.Items.Add(li);

 li = new ListItem("bbbbb", "bbbbbbb");
 FolloupRadioButtonList.Items.Add(li);

 li.Text += @"<table style='padding-left:50px'>
                        <tr>
                            <td><input id='ChildList_0' type='radio' name='ChildList' value='zzzzzzz' /><label for='ChildList_0'>zzzzzzz</label></td>
                        </tr>
                        <tr>
                            <td><input id='ChildList_1' type='radio' name='ChildList' value='xxxxxxx' /><label for='ChildList_1'>xxxxxx</label></td>
                        </tr>
              </table>";

Will produce this list and sublist:

Sub radibuttonlist

But it won't be truly server-side control. If you need just client-side access to this list - this approach may work for you. Otherwise, as I mentioned in my comments - consider a user control where you can build your own hiearchy out of ASP.NET tables and RadioButtons.

like image 30
Yuriy Galanter Avatar answered Jan 19 '26 18:01

Yuriy Galanter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!