ASP.NET RadioButton messing with the name (groupname)(ASP.NET RadioButton 与名称(组名)混淆)
问题描述
我有一个模板化控件(中继器),列出了一些文本和其他标记.每个项目都有一个与之关联的单选按钮,使用户可以选择中继器创建的项目之一.
I got a templated control (a repeater) listing some text and other markup. Each item has a radiobutton associated with it, making it possible for the user to select ONE of the items created by the repeater.
转发器写入单选按钮,设置其 id 和生成的名称,使用默认的 ASP.NET 命名约定,使每个单选按钮成为一个完整的组".这意味着所有单选按钮都是相互独立的,不幸的是,这再次意味着我可以同时选择所有单选按钮.单选按钮具有用于设置通用名称的巧妙属性组名",因此它们被组合在一起,因此应该相互依赖(所以我一次只能选择一个).问题是 - 这不起作用 - 转发器确保 id 和名称(控制分组)不同.
The repeater writes the radiobutton setting its id and name generated with the default ASP.NET naming convention making each radiobutton a full 'group'. This means all radiobuttons are independent on each other, which again unfortunately means I can select all radiobuttons at the same time. The radiobutton has the clever attribute 'groupname' used to set a common name, so they get grouped together and thus should be dependant (so I can only select one at a time). The problem is - this doesn't work - the repeater makes sure the id and thus the name (which controls the grouping) are different.
由于我使用了中继器(可能是列表视图或任何其他模板化数据绑定控件),我无法使用 RadioButtonList.那我该怎么办?
Since I use a repeater (could have been a listview or any other templated databound control) I can't use the RadioButtonList. So where does that leave me?
我知道我以前遇到过这个问题并解决了它.我知道几乎每个 ASP.NET 程序员都必须拥有它,那么为什么我不能 google 并找到解决问题的可靠方法呢?我遇到了通过 JavaScript 强制分组的解决方案(丑陋!),甚至将单选按钮作为非服务器控件处理,迫使我执行 Request.Form[name]
来读取状态.我还尝试在 PreRender
事件上覆盖 name 属性 - 不幸的是,拥有页面和 masterpage 再次覆盖此名称以反映完整的 id/name,所以我最终得到了相同的错误结果.
I know I've had this problem before and solved it. I know almost every ASP.NET programmer must have had it too, so why can't I google and find a solid solution to the problem? I came across solutions to enforce the grouping by JavaScript (ugly!) or even to handle the radiobuttons as non-server controls, forcing me to do a Request.Form[name]
to read the status. I also tried experimenting with overriding the name attribute on the PreRender
event - unfortunately the owning page and masterpage again overrides this name to reflect the full id/name, so I end up with the same wrong result.
如果您没有比我发布的更好的解决方案,仍然非常欢迎您发表您的想法 - 至少我会知道我的朋友 'jack' 关于 ASP.NET 有时是多么混乱的说法是正确的 ;)
If you have no better solution than what I posted, you are still very welcome to post your thoughts - at least I'll know that my friend 'jack' is right about how messed up ASP.NET is sometimes ;)
推荐答案
Google-fu: asp.net单选按钮转发器问题
Google-fu: asp.net radiobutton repeater problem
确实是 id 修改的不幸后果.我的想法是创建一个 - 或从众多可用的 - 自定义控件中选择一个,以在客户端上添加对同名的支持.
Indeed an unfortunate consequence of the id mangling. My take would be creating a - or picking one of the many available - custom control that adds support for same name on the client.
这篇关于ASP.NET RadioButton 与名称(组名)混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET RadioButton 与名称(组名)混淆
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01