2

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 (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();
   }
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
James Boer
  • 321
  • 4
  • 9
  • 28
  • http://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c use linq – M.kazem Akhgary Sep 27 '15 at 04:37
  • I tried using List selected = CheckBoxList1.Items.Cast() .Where(li => li.Selected) .ToList(); but its not working like in my if selected !=null – James Boer Sep 27 '15 at 04:46
  • @newtoasp Use `checked` property of checkbox instead of selectedvalue property... – Amnesh Goel Sep 27 '15 at 04:46
  • 1
    http://stackoverflow.com/questions/3486847/checking-if-checkboxlist-has-any-selected-values answer is here.. – Amnesh Goel Sep 27 '15 at 05:08
  • Possible duplicate of [Checking if CheckBoxList has any selected values](https://stackoverflow.com/questions/3486847/checking-if-checkboxlist-has-any-selected-values) – TylerH Apr 30 '19 at 15:29
  • Note you've got a typo in your question. Where it says `LABEL1.Text = ("Please select at least one checkbox();`, you've put an open bracket at the end `(` where you should have a quotation mark `"`. The SO syntax editor gives away that something's not right here - everything below looks like text! It's strange that multiple people have posted answers to this question and at least 3 people have reproduced the same typo without checking it ... – Lou Jan 27 '23 at 15:41

4 Answers4

3

Use linq Any

if (IsPostBack)
{
    bool selected1 = CheckBoxList1.Items.Cast<ListItem>().Any(li => li.Selected);
    bool selected2 = 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();
    }

Use || operator if you want atleast one check item. not matter from which list.

if (selected1 || selected2) // true if at least 1 item is checked
{
    Bind();
}
Lou
  • 2,200
  • 2
  • 33
  • 66
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • You can use your earlier method along with `ANY`. This will help to other users who will see this answer in future.. – Amnesh Goel Sep 27 '15 at 05:13
  • what do you mean by any? and it does work as it still goes into bind statement even when nothing is checked – James Boer Sep 27 '15 at 05:44
  • here `Any` will check trough all items from ckeckboxlist and will return `true` if any of those are selected. @newtoasp – M.kazem Akhgary Sep 27 '15 at 05:56
  • check with debugger. it seems there is another problem exist too. you probably have to ask a new question about that if you find it and wasnt able to fix it. @newtoasp – M.kazem Akhgary Sep 27 '15 at 05:59
1

You can use Count property to determine whether any item was selected from ComboBoxList or not. Count will return the no of items selected, and if you have not marked any selection then this property will return 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();
   }
}
Lou
  • 2,200
  • 2
  • 33
  • 66
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • `CheckBoxList` doesn't have a `Checked` property. – juharr Sep 27 '15 at 05:04
  • You also need to do a `.Cast` because `CheckBox.Items` is a `ListItemCollection` which implements `IEnumerable` and not `IEnumerable`. – juharr Sep 27 '15 at 05:12
  • Weird how many people reposted OP's code and didn't catch the typo in it. Just edited this. One line was missing a close quotation mark. – Lou Jan 27 '23 at 15:42
1

Checkbox value will not be null, so you need to check only if the values are empty, like this :

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();
       }
    }
Lou
  • 2,200
  • 2
  • 33
  • 66
Ayman Barhoum
  • 1,255
  • 1
  • 16
  • 31
  • Hi, Still goes to the bind statement even when I uncheck all for checkboxlist 1. please advice – James Boer Sep 27 '15 at 05:03
  • it seems you have selected value in CheckBoxList1, can you debug the code or try : `Response.Write(CheckBoxList1.SelectedValue);` just to make sure there is no selected values in checkboxlist1 ? – Ayman Barhoum Sep 27 '15 at 05:12
  • If one of both checkboxlist values is empty, the bind() will not be executed, but I think there is another code that do the binding, try to hide the grid in the else statement, like this : `GridView1.Visible = false;` – Ayman Barhoum Sep 27 '15 at 06:07
  • Fixed the typo which came from OP's original post. – Lou Jan 27 '23 at 15:43
1

the If(IsPostback) I think is the culprit. If your page has been refreshed by a button (PostBack) then your checkbox list will Bind(). So everytime you click a button anywhere in the page, your list gets refreshed which makes your selected boxes removed.

Try to change the If(IsPostBack) into If(!IsPostBack)

EDIT:

Oh got it, your .SelectedValue is a string, therefore its never a null.

Change this

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

to this

if(CheckBoxList1.SelectedValue != String.Empty && CheckBoxList2.SelectedValue != String.Empty)

and revert back the If(!IsPostBack) to If(IsPostBack) as it seems this code event is under a button_click or something, not the thing I assumed as PageLoad.

please approach for concerns. thanks

ken lacoste
  • 894
  • 8
  • 22