ASP.NET c# - RadioButtons 未分组

发布于 2024-10-05 03:24:33 字数 702 浏览 3 评论 0原文

我有一个 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 没有将输入的名称属性设置为 GroupName。所以我可以选择任意数量的单选按钮。这不是我想要发生的事情。我希望能够在由 GroupName 标识的组中选择一个且仅一个。

I have a GridView and in its OnRowDataBound callback I am adding a new cell and to that cell I'm adding a RadioButton. I've set the GroupName on the radio button, but I can still select more than one button at a time.

How can I make them grouped so that only one radio button can be selected within the group.

EDIT - This is my code:

    // 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);

So I'm setting a unique ID and the same GroupName. ASP.NET is NOT setting the name attribute of the input to the GroupName. So I can select as many radio buttons as I want. This is not what I want to happen. I want to be able to select one and only one in the group identified by the GroupName.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-10-12 03:24:33

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 在转发器内命名事物的方式,组属性中的名称不再完全相同(本质上每个控件都会获得一个前缀以使其唯一)。

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

Basically, when in a repeater of any kind (which a GridView is), the names are no longer all the same from the group property as a result of the way asp.net names things inside a repeater (essentially each control gets a prefix to make it unique).

静若繁花 2024-10-12 03:24:33

您确定要为代码隐藏中的单选按钮指定不同的名称吗?

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);
            }

Are you sure you are giving different names to the radiobuttons on codebehind?

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);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文