【问题标题】:ASP.NET RadioButtonList: the element 'br' cannot be nested within the element 'listitem'ASP.NET RadioButtonList:元素“br”不能嵌套在元素“listitem”中
【发布时间】:2016-05-03 06:58:51
【问题描述】:

例如我有这个单选按钮。

(o) item1  (o) item 2

但是,我希望它是这样的:

(o) item1   (o) item2
  line here   line here

我当前的标记是:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Width="650px" Font-Size="Smaller">
    <asp:ListItem Selected="True" Value="0">Relax<br>(MY MAN)</asp:ListItem>
    <asp:ListItem Value="1">Relax Again<br>(My Man)</asp:ListItem>
    <asp:ListItem Value="2">Relax Again 2<br>(MyMan)</asp:ListItem>
    <asp:ListItem Value="3">Relax 3<br>(My Man)</asp:ListItem>
    <asp:ListItem Value="4">Relax 4<br>(My Man)</asp:ListItem>
    <asp:ListItem Value="5">Relax 5<br>(My Man)</asp:ListItem>
    <asp:ListItem Value="6">Relax 6<br>(My Man)</asp:ListItem>
</asp:RadioButtonList>

但它会生成以下验证错误消息:the element 'br' cannot be nested within the element 'listitem'

【问题讨论】:

  • 没有尝试过,然后一直收到相同的消息。
  • 哈哈哈:我正要写答案。 :-)
  • 哈哈,你真棒!
  • 我发布了我的答案。尽管您的标记显示错误,但它确实有效,不是吗?
  • 是的,它以应有的方式完美运行,但随后再次收到这些黄色警告,这是我试图避免的。

标签: asp.net radiobuttonlist


【解决方案1】:

注意:这里给出的解决方案确实有效,但比必要的复杂。请参阅 J Talati 对他自己的帖子的回答以获得最佳答案。


如果您想要一个没有错误的标记,您可以在一个表中重现 RadioButtonList,其中单独的 RadioButtons 共享相同的GroupName(这是 RadioButtonList 呈现的方式):
<table id="RadioButtonTable1" runat="server" class="radioList">
    <tr>
        <td>
            <asp:RadioButton runat="server" GroupName="RadioList" Checked="true" Value="0" Text="Relax<br />(MY MAN)" />
        </td>
        <td>
            <asp:RadioButton runat="server" GroupName="RadioList" Value="1" Text="Relax Again<br />(My Man)" />
        </td>
        <td>
            <asp:RadioButton runat="server" GroupName="RadioList" Value="2" Text="Relax Again 2<br />(MyMan)" />
        </td>
        ...
    </tr>
</table>

radioList 类样式允许自定义布局:

.radioList td
{
    text-align: left;
    width: 80px;
}

现在,获取选定的值并不像 RadioButtonList 那样简单。可以在Better way to find control in ASP.NET使用Jimmy提供的类:

ControlFinder<RadioButton> radios = new ControlFinder<RadioButton>();
radios.FindChildControlsRecursive(RadioButtonTable1);
foreach (RadioButton rb in radios.FoundControls)
{
    if (rb.Checked)
    {
        string value = rb.Attributes["Value"];
        ...
        break;
    }
}

RadioButtons 没有 Value 属性,但该属性可以添加到标记中并在代码隐藏中检索,如所示。

【讨论】:

  • 嘿,我发布了一个答案,并找到了一个解决我的代码的工作。谢谢,伙计!
【解决方案2】:
<asp:radiobuttonlist id="RadioButtonList1" runat="server" repeatdirection="Horizontal" width="650px" font-size="Smaller" xmlns:asp="#unknown">
<asp:listitem selected="True" value="0" text="Relax <br>(MY MAN)"></asp:listitem>
<asp:listitem value="1" text="Relax Again<br>(My Man)"></asp:listitem>
<asp:listitem value="2" text="Relax Again 2<br>(MyMan)"></asp:listitem>
<asp:listitem value="3" text="Relax 3<br>(My Man)"></asp:listitem>
<asp:listitem value="4" text="Relax 4<br>(My Man)"></asp:listitem>
<asp:listitem value="5" text="Relax 5<br>(My Man)"></asp:listitem>
<asp:listitem value="6" text="Relax 6<br>(My Man)"></asp:listitem>

让这个工作!呜呼!只需添加文本属性即可。

【讨论】:

猜你喜欢
  • 2014-05-22
  • 1970-01-01
  • 2017-07-27
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
相关资源
最近更新 更多