ASP.NET c# - RadioButtons未分组

时间:2010-12-01 21:53:28

标签: c# asp.net-2.0 radio-button

我有一个GridView,在其OnRowDataBound回调中我添加了一个新单元格,并且我正在添加一个RadioButton。我在单选按钮上设置了GroupName,但我仍然可以一次选择多个按钮。

如何对它们进行分组,以便在组中只能选择一个单选按钮。

编辑 - 这是我的代码:

    // Add the radio button to select if this purchased address
    // is set the the home address of the contact
    e.Row.Cells.AddAt(0, new TableCell());
    RadioButton rbSetAddress = new RadioButton();
    rbSetAddress.ID = "rdSetAddress" + e.Row.Cells[2].Text;
    rbSetAddress.GroupName = "SetAddress";
    e.Row.Cells[0].Controls.Add(rbSetAddress);

所以我设置了一个唯一的ID和相同的GroupName。 ASP.NET不会将输入的name属性设置为GroupName。所以我可以选择任意数量的单选按钮。这不是我想要发生的事情。我希望能够在GroupName标识的组中选择一个且仅一个。

2 个答案:

答案 0 :(得分:1)

http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx

http://www.developer.com/net/asp/article.php/3623096/ASPNET-Tip-Using-RadioButton-Controls-in-a-Repeater.htm

基本上,当在任何类型的转发器(GridView是)中时,由于asp.net在转发器中命名事物的方式(基本上每个控件获得),因此组属性中的名称不再完全相同使其独一无二的前缀。

答案 1 :(得分:0)

您确定要为代码背后的单选按钮指定不同的名称吗?

for (int i = 0; i < 10; i++)
            {
                RadioButton rb = new RadioButton();
                rb.ID = "rb"+i;
                rb.GroupName="rbGroup";
                GridView1.Rows[i].Cells[3].Controls.Add(rb);
            }