如何检查是否至少选中了一个复选框?

编程入门 行业动态 更新时间:2024-10-23 13:25:51
本文介绍了如何检查是否至少选中了一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是在每个复选框列表中选中至少一个复选框时才尝试将我的数据绑定到gridview。然而,它似乎没有工作,因为当我点击提交它没有复选框选中它仍然进入绑定语句,并没有在标签中显示文本消息。 我的代码出了什么问题?请帮助

Hi, Im trying to bind my data into gridview only when at least one checkbox is checked in each checkboxlist. However it does not seem to work as when I click on submit it with no checkbox checked it still go in the bind statement and did not display the text message in the label. Where did it gone wrong in my code? please help

if (IsPostBack) { if (!stringIsNullOrEmpty(CheckBoxList1.SelectedValue) && !stringIsNullOrEmpty(CheckBoxList2.SelectedValue) ) { Bind(); } else if (CheckBoxList1.SelectedValue == String.Empty) { LABEL1.Text = ("Please select at least one checkbox").ToString(); } else if (CheckBoxList2.SelectedValue == String.Empty) { LABEL2.Text = ("Please select at least one checkbox").ToString(); }

推荐答案

简单,如果你的代码总是 Bind ,这只是因为 CheckBoxList1.SelectedValue 不返回 null 是否选中了一个方框。 使用调试器并查看 CheckBoxList1.SelectedValue 返回的值和类型。 我想知道这段代码是否编译?更新的代码现在必须编译。 我后来看到你比较 CheckBoxList1.SelectedValue 针对 String.Empty ,如果它在第二次测试中有效,它也应该在第一次测试中工作。 /> 我建议阅读文档以了解 CheckBoxList1.SelectedValue Simple, if your code always Bind, it is simply because CheckBoxList1.SelectedValue do not return null whether a box is checked or not. Use a debugger and look at the value and type returned by CheckBoxList1.SelectedValue. I wonder of this code compile ? updated code must compile now. I see that later, you compare CheckBoxList1.SelectedValue against String.Empty, if it work in second test, it should also work in first test. I suggest to read the documentation to know the type returned by CheckBoxList1.SelectedValue

返回的类型

更多推荐

如何检查是否至少选中了一个复选框?

本文发布于:2023-11-04 00:59:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!