Click here to Skip to main content
15,884,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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();
                       }
Posted
Updated 26-Sep-15 19:25pm
v2

1 solution

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
 
Share this answer
 
v3
Comments
waynetan123 27-Sep-15 0:09am    
In debug !=null is true even when I did not check any item. please suggest alternative thanks
Patrice T 27-Sep-15 0:17am    
see answer update
waynetan123 27-Sep-15 0:32am    
still cant
Patrice T 27-Sep-15 0:43am    
and what is saying the documentation ?
waynetan123 27-Sep-15 0:49am    
what documentation are you referring to?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900