有什么简单的方法来检查复选框列表控件中是否没有选择任何项目?
I know I can loop through a checkboxlist.Items
and see if none of them is selected, but is there a better way to find out if no items have been selected, in the code behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须检查 SelectedIndex。 If it equals -1, its means nothing is selected.
You have to check the SelectedIndex. If it equals -1, its means nothing is selected.
CheckBoxList 具有 属性SelectedIndex 和 SelectedValue。
可以检查是否有SelectedIndex;如果没有检查任何项目,它将是-1。
CheckBoxList has properties for SelectedIndex and SelectedValue.
You can check whether there is a SelectedIndex; it will be -1 if no items are checked.
这可能是验证 CheckBoxList 的最简单方法:
使用自定义验证器:
创建 JavaScript 函数来验证 CheckBoxList:
另一种选择是创建自定义验证控件,如下所示:
This is probably the easiest way to validate the CheckBoxList:
Use a custom validator:
Create a JavaScript function to validate the CheckBoxList:
Another option is to create a custom validation control, like this:
您必须检查所选索引。如果所选索引为-1,则表示没有选择任何项目。
You have to check selected index. If selected index is -1 it means that no item is being selected.