ASP.NET c# - RadioButtons 未分组
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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).
您确定要为代码隐藏中的单选按钮指定不同的名称吗?
Are you sure you are giving different names to the radiobuttons on codebehind?