Click here to Skip to main content
16,015,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the following code. when I select all "yes" then I get a message "you have completed succesfully" which is right. But when I select all No then when submit, in the 2nd 3rd 4th and 5th buttton list the "No" is automatically turning to "Yes" but only in the 1st button list it stays "No" which is right. Why the others turning to yes?

C#
public bool isCheckboxSelected1()
                {
                    for (int i = 0; i <radiobuttonlist1.items.count;>                    {

                        if (RadioButtonList1.Items[0].Selected )
                        {
                            return true;
                        }
                    }
                    return false;
                }
                public bool isCheckboxSelected2()
                {
                    for (int i = 0; i < RadioButtonList2.Items.Count; i++)
                   {

                      if (RadioButtonList2.Items[0].Selected)
                      {
                        return true;
                      }
                  }
               return false;
             }
             public bool isCheckboxSelected3()
             {
                 for (int i = 0; i < RadioButtonList3.Items.Count; i++)
                 {

                     if (RadioButtonList3.Items[0].Selected)
                     {
                         return true;
                     }
                 }
                 return false;
             }
             public bool isCheckboxSelected4()
             {
                 for (int i = 0; i < RadioButtonList4.Items.Count; i++)
                 {

                     if (RadioButtonList4.Items[0].Selected)
                     {
                         return true;
                     }
                 }
                 return false;
             }
             public bool isCheckboxSelected5()
             {
                 for (int i = 0; i < RadioButtonList5.Items.Count; i++)
                 {

                     if (RadioButtonList5.Items[0].Selected)
                     {
                         return true;
                     }
                 }
                 return false;
             }
                protected void Button1_Click(object sender, EventArgs e)
                {

                    if (isCheckboxSelected1() && isCheckboxSelected2() && isCheckboxSelected3() && isCheckboxSelected4() && isCheckboxSelected5())
                    {
                        Label1.Text = string.Format(" You have completed  successfully");
                    }
                    else
                    {
                        Label1.Text = string.Format(" please select each box ");
                    }
                }
Posted
Updated 18-Aug-11 3:46am
v3
Comments
Sergey Alexandrovich Kryukov 19-Aug-11 0:36am    
Look properly at what you have posted. It won't even compile. However, it's the artifact of HTML. Please format it properly. You cannot use angular brackets (less, greater characters) in HTML, use &gt;, &lt;.
--SA

Your problem is your indexor.

C#
 for (int index = 0;
        index < radiobuttonlist1.items.count;
        index++)
{

    if (RadioButtonList1.Items[index].Selected )
    {
        return true;
    }
}
return false;


In your code you are always looking at index 0 in your loop!!!
I trust also that you want to return true when at least 1 of your buttons is clicked in each list.

A better and cleaner way of implementing your code is as follows:

C#
public bool IsRadioButtonSelected( RadioButtonList list )
{
     for ( int index = 0; index < list.count; index++)
     {
          if ( list.Items[index].Selected )
               return true;
     }
     return false;
}


And in your use of this you would say:


C#
if ( IsRadioButtonSelected(radioList1) && IsRadioButtonSelected(radioList2)...
 
Share this answer
 
I'm not sure I fully understand your question as your scenario is not very clear, but it is important to understand the different operators.

The && operator[^]

Hope that helps some...please try to clarify your question!

Cheers.
 
Share this answer
 

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