【问题标题】:How can I group radio buttons in a table?如何在表格中对单选按钮进行分组?
【发布时间】:2013-06-14 08:26:36
【问题描述】:

我正在使用嵌套中继器在表格中动态构建单选按钮列表:

 <ItemTemplate>
                 <tr>
                     <td>
                        <asp:Label ID="lblAccID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                     </td>
                     <td>
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                     </td>
                     <td>  
                            <%-- POPULATE CONDITION RADIO BUTTONS --%>
                            <asp:Repeater ID="rpConditionRadioButtons" runat="server">
                                <ItemTemplate>
                                    <td>
                                        <asp:RadioButton ID="rbCondition" GroupName="gCondition" Text="" runat="server" />
                                    </td>
                                </ItemTemplate>
                            </asp:Repeater>
                     </td>
                </tr>
            </ItemTemplate>

但我不知道如何将它们组合在一起。 (因为表格是动态构建的,所以我认为我不能使用单选按钮列表。)

我尝试使用 GroupName 属性,并将 ClientIDMode 设置为静态,但这没有帮助。

【问题讨论】:

  • 我遇到了相反的问题 - 而不是所有单选按钮都在转发器中的所有行中分组,它们是独立运行的。

标签: c# asp.net radio-button


【解决方案1】:

如果你嵌套一个中继器,你还有一个父 RepeaterItem 具有唯一标识符(例如主键)。您可以将其动态附加到内部中继器的ItemDataBound 中的GroupName 属性中。您可以通过以下方式从内部中继器的ItemDataBound 访问外部中继器的当前RepeaterItem(假设lblAccID.Text 包含ID):

protected void rpConditionRadioButtons_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater innerRepeater = (Repeater)sender;
        RepeaterItem outerItem = (RepeaterItem)innerRepeater.NamingContainer;
        Label lblAccID = (Label)outerItem.FindControl("lblAccID");
        RadioButton rbCondition = (RadioButton) e.Item.FindControl("rbCondition");
        rbCondition.GroupName = "gCondition_" + lblAccID.Text;
    }
}

评论:

OK - 找到单选按钮,并且可以更改它们的属性,但是 他们仍然独立工作。

恐怕这是known bug

请查看此问题以获得更多帮助:asp.net radio button grouping

【讨论】:

猜你喜欢
  • 2011-01-23
  • 2011-11-01
  • 2013-01-25
  • 2013-06-02
  • 2012-06-12
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多