如何检查是否至少有一个 RadioButtonList 选择了一项?
我在一个页面上有 20 个 RadioButtonList
。
我需要创建一种验证方法,以确保这些 RadioButtonList 中至少有一个已选定项目。
我需要为此使用什么样的验证?
I have 20 RadioButtonList
s on a page.
I need to create a validation method to ensure that at least one of these RadioButtonList
s has an item selected.
What kind of validation would I need to use for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:根据评论和澄清更新了问题。
如果您要针对多个
RadioButtonList
进行验证,那么您需要使用CustomValidator
并实现服务器端检查。以下是一些测试标记:
使用以下扩展方法查找所有
RadioButtonList
控件(源) :然后实现服务器端
CustomValidator
检查:我已经测试了上面的示例,它似乎完全符合您的需要。您应该能够很容易地将其切换到 VB。希望这能解决您的问题。
EDIT: Updated question based on comments and clarification.
If you are validating against multiple
RadioButtonList
s then you need to use aCustomValidator
and implement the server side check.Here is some test markup:
Use the following extension method to find all the
RadioButtonList
controls (Source):Then implement the server side
CustomValidator
check:I have tested the above example and it seems to do exactly what you need. You should be able to switch it to VB pretty easily. Hope this solves your problem.
您可以为 RadioButtonList 设置默认值,这意味着用户永远无法不选择选项,并且您不需要所有验证代码
编辑
正如下面的评论所指出的,这本身不足以作为验证手段。在服务器端验证所有用户输入也是一种很好的做法。
You could set a default for your RadioButtonList which would mean a user could never not select an option and you would not need all the validation code
EDIT
As pointed out in a comment below this would not be sufficient by itself as a means of validation. It is good practice to validate all user input on the server-side also.
我使用适用于 ListControls 的扩展方法
I use an extension method that works for ListControls