单选按钮列表出现问题 [英] Issue with radio button list

查看:87
本文介绍了单选按钮列表出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中.当我选择所有是"时,我会收到一条消息您已成功完成",这是正确的.但是,当我选择全部否,然后提交时,在第二,第三,第四和第五个按钮列表中,否"会自动变为是",但只有在第一个按钮列表中,它会保持为否",这是正确的.为什么其他人转向是?

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?

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 ");
                    }
                }

推荐答案

您的问题是您的索引器.

Your problem is your indexor.

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

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



在您的代码中,您始终会在循环中查看索引0! 我也相信,当您在每个列表中至少单击一个按钮时,您想返回true.

更好,更干净的代码实现方式如下:



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:

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:


if ( IsRadioButtonSelected(radioList1) && IsRadioButtonSelected(radioList2)...


我不确定我是否完全理解您的问题,因为您的情况不是很清楚,但是了解不同的运算符很重要.

The&&运算符 [ ^ ]

希望对您有所帮助...请尝试澄清您的问题!

干杯.
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.


这篇关于单选按钮列表出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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