如何检查是否至少选中了一个复选框? [英] How to I check if at least one checkbox is checked?

查看:114
本文介绍了如何检查是否至少选中了一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在每个复选框列表中选中至少一个复选框时才尝试将我的数据绑定到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

返回的类型

这篇关于如何检查是否至少选中了一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆