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

C# 如何检查是否至少选中了一个复选框?,c#,asp.net,checkboxlist,C#,Asp.net,Checkboxlist,只有在每个复选框列表中至少选中一个复选框时,我才尝试将数据绑定到gridview。但是,当我单击Submitititwithnotcheckbox时,它似乎并没有工作,它仍然在bind语句中,并且没有在标签中显示文本消息 我的代码哪里出错了?请帮忙 if (IsPostBack) { if (CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null) { Bind(

只有在每个复选框列表中至少选中一个复选框时,我才尝试将数据绑定到gridview。但是,当我单击Submitititwithnotcheckbox时,它似乎并没有工作,它仍然在bind语句中,并且没有在标签中显示文本消息

我的代码哪里出错了?请帮忙

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

使用linq
Any

if (IsPostBack)
{
    bool seleceted1 = CheckBoxList1.Items.Cast<ListItem>().Any(li => li.Selected);
    bool seleceted2 = CheckBoxList2.Items.Cast<ListItem>().Any(li => li.Selected);

    if (selected1 && selected2)
    {
       Bind();
    }
    else if (!selected1)
    {
       LABEL1.Text = ("Please select at least one checkbox();
    }
    else if (!selected2)
    {
       LABEL2.Text = ("Please select at least one checkbox").ToString();
    }

使用linq
Any

if (IsPostBack)
{
    bool seleceted1 = CheckBoxList1.Items.Cast<ListItem>().Any(li => li.Selected);
    bool seleceted2 = CheckBoxList2.Items.Cast<ListItem>().Any(li => li.Selected);

    if (selected1 && selected2)
    {
       Bind();
    }
    else if (!selected1)
    {
       LABEL1.Text = ("Please select at least one checkbox();
    }
    else if (!selected2)
    {
       LABEL2.Text = ("Please select at least one checkbox").ToString();
    }

您可以使用Count属性来确定是否从ComboBoxList中选择了任何项。Count将返回所选项目的数量,如果未标记任何选择,则此属性将返回0

if (IsPostBack)
{
   if (CheckBoxList1.Items.Cast<ListItem>().Count(li => li.Selected) != 0 && 
       CheckBoxList2.Items.Cast<ListItem>().Count(i => i.Selected) != 0)
   {
      Bind();
   }
   else if (!CheckBoxList1.Checked)
   {
      LABEL1.Text = ("Please select at least one checkbox();
   }
   else if (!CheckBoxList2.Checked)
   {
      LABEL2.Text = ("Please select at least one checkbox").ToString();
   }
}
if(IsPostBack)
{
if(CheckBoxList1.Items.Cast().Count(li=>li.Selected)!=0&&
CheckBoxList2.Items.Cast().Count(i=>i.Selected)!=0)
{
Bind();
}
如果(!CheckBoxList1.选中),则为else
{
LABEL1.Text=(“请至少选择一个复选框();
}
如果(!CheckBoxList2.选中),则为else
{
LABEL2.Text=(“请至少选择一个复选框”).ToString();
}
}

您可以使用Count属性来确定是否从ComboBoxList中选择了任何项目。Count将返回所选项目的数量,如果未标记任何选择,则此属性将返回0

if (IsPostBack)
{
   if (CheckBoxList1.Items.Cast<ListItem>().Count(li => li.Selected) != 0 && 
       CheckBoxList2.Items.Cast<ListItem>().Count(i => i.Selected) != 0)
   {
      Bind();
   }
   else if (!CheckBoxList1.Checked)
   {
      LABEL1.Text = ("Please select at least one checkbox();
   }
   else if (!CheckBoxList2.Checked)
   {
      LABEL2.Text = ("Please select at least one checkbox").ToString();
   }
}
if(IsPostBack)
{
if(CheckBoxList1.Items.Cast().Count(li=>li.Selected)!=0&&
CheckBoxList2.Items.Cast().Count(i=>i.Selected)!=0)
{
Bind();
}
如果(!CheckBoxList1.选中),则为else
{
LABEL1.Text=(“请至少选择一个复选框();
}
如果(!CheckBoxList2.选中),则为else
{
LABEL2.Text=(“请至少选择一个复选框”).ToString();
}
}

复选框值不会为空,因此仅当值为空时才需要检查,如下所示:

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

复选框值不会为空,因此仅当值为空时才需要进行检查,如下所示:

if (!string.IsNullOrEmpty( CheckBoxList1.SelectedValue)  && !string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
               {
                  Bind();
               }
    else 
    {
    if (string.IsNullOrEmpty( CheckBoxList1.SelectedValue))
       {
          LABEL1.Text = ("Please select at least one checkbox();
       }
       else if (string.IsNullOrEmpty( CheckBoxList2.SelectedValue))
       {
          LABEL2.Text = ("Please select at least one checkbox").ToString();
       }
    }
If(IsPostback)
我认为是罪魁祸首。如果您的页面已通过按钮(PostBack)刷新,则您的复选框列表将绑定()。因此,每次单击页面中任何位置的按钮时,您的列表都会刷新,从而删除选定的框

尝试将
If(IsPostBack)
更改为
If(!IsPostBack)

编辑:

哦,明白了,你的.SelectedValue是一个字符串,因此它永远不会为null

改变这个

if(CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
对此

if(CheckBoxList1.SelectedValue != String.Empty && CheckBoxList2.SelectedValue != String.Empty)
然后将
If(!IsPostBack)
还原为
If(IsPostBack)
,因为此代码事件似乎位于
按钮下(单击
或其他什么东西),而不是我假设的
页面加载

请联系解决问题。谢谢如果(IsPostback)我认为是罪魁祸首。如果您的页面已通过按钮(PostBack)刷新,则您的复选框列表将绑定()。因此,每次单击页面中任何位置的按钮,您的列表都会刷新,从而删除所选框

尝试将
If(IsPostBack)
更改为
If(!IsPostBack)

编辑:

哦,明白了,你的.SelectedValue是一个字符串,因此它永远不会为null

改变这个

if(CheckBoxList1.SelectedValue != null && CheckBoxList2.SelectedValue != null)
对此

if(CheckBoxList1.SelectedValue != String.Empty && CheckBoxList2.SelectedValue != String.Empty)
然后将
If(!IsPostBack)
还原为
If(IsPostBack)
,因为此代码事件似乎位于
按钮下(单击
或其他什么东西),而不是我假设的
页面加载

请联系我们以了解您的担忧。谢谢您的简单建议

    for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++)
    {
        if (RadioButtonList1.Items[i].Selected==true)
        {
            //Bind here
        }
    }
for(int i=0;i
简单的一个

    for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++)
    {
        if (RadioButtonList1.Items[i].Selected==true)
        {
            //Bind here
        }
    }
for(int i=0;i
use-linqI尝试使用List-selected=CheckBoxList1.Items.Cast().Where(li=>li.selected.ToList());但如果选中,它的工作方式与我的中的不同=null@newtoasp使用复选框的
checked
property而不是selectedvalue属性…答案在这里..可能重复使用linqI尝试使用List selected=CheckBoxList1.Items.Cast()。其中(li=>li.selected.ToList());但如果选中,它的工作方式与我的中的不同=null@newtoasp使用复选框的
checked
property而不是selectedvalue属性…答案在这里..Hi可能重复,即使我取消选中复选框列表1的all,仍然会转到bind语句。请注意,您似乎在复选框列表1中选择了值,您可以调试cod吗e或尝试:
Response.Write(CheckBoxList1.SelectedValue);
只是为了确保CheckBoxList1中没有选定的值?如果两个checkboxlist值中的一个为空,则bind()将不会执行,但我认为还有另一个代码可以执行绑定,请尝试在else语句中隐藏网格,如:
GridView1.Visible=false;
Hi,即使我取消选中复选框列表1的所有复选框,仍然会转到bind语句。请注意,您似乎已经在复选框列表1中选择了值,您可以调试代码或尝试吗:
Response.Write(CheckBoxList1.SelectedValue);
只是为了确保CheckBoxList1中没有选定的值?如果两个checkboxlist值中的一个都为空,则bind()将不会执行,但我认为还有另一个代码可以执行绑定,请尝试在else语句中隐藏网格,如:
GridView1.Visible=false;
CheckBoxList
没有
Checked
属性。您还需要执行
.Cast
,因为
CheckBox.Items
是一个
列表项集合在
上,实现
IEnumerable
而不是
IEnumerable
复选框列表没有选中的
属性。您还需要执行
.Cast
,因为
复选框。Items
是实现
IEnumerable的
列表项集合