【问题标题】:How to add items between RadioButtonList?如何在 RadioButtonList 之间添加项目?
【发布时间】:2014-01-28 22:54:56
【问题描述】:

我们有一个在服务器端生成的列表,然后使用<asp:RadioButtonList... 显示它。

但是,我们如何在其中一项之后添加一些 HTML?

这个想法是,如果生成其中一个,就会生成一些“子”单选按钮。

谢谢

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>

【问题讨论】:

  • 粘贴您目前拥有的内容。
  • @Esa 粘贴了上面的代码。
  • 如果你需要层次结构,也许 RadioButtonList 不是最好的方法。树或自定义用户控件可能是更好的选择

标签: asp.net


【解决方案1】:

创建您自己的自定义单选按钮列表控件:

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

并照此使用。

首先将控件注册到页面,或者用户控件:

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

然后使用控件:

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

它是服务器端的一个基本单选按钮列表控件,所以就像现在一样使用它。

【讨论】:

    【解决方案2】:

    从技术上讲,您可以将您的 HTML 直接分配给 ListItem 文本,例如这段代码

     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>";
    

    将产生这个列表和子列表:

    但它不会是真正的服务器端控制。如果您只需要客户端访问此列表 - 这种方法可能适合您。否则,正如我在 cmets 中提到的 - 考虑一个用户控件,您可以在其中使用 ASP.NET 表和 RadioButtons 构建自己的层次结构。

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 2015-03-20
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2023-01-12
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多