0

I have around 75 radio buttons (before you ask why you need that many radio buttons, lets just say I need them), I have grouped them by 5 in different groups. So, my question is, is there a way I can validate them if in each group at least one radio button is selected before I submit something. SO I have 15 groups like this.

<h4 style="">1 Question? </h4>

<asp:RadioButton ID="RadioButton1"  runat="server" Text="Strongly Disagree" GroupName="Gr1" />
<asp:RadioButton ID="RadioButton2"  runat="server" Text="Disagree" GroupName="Gr1" />
<asp:RadioButton ID="RadioButton3"  runat="server" Text="Uncertain" GroupName="Gr1" />
<asp:RadioButton ID="RadioButton4"  runat="server" Text="Agree" GroupName="Gr1" />
<asp:RadioButton ID="RadioButton5"  runat="server" Text="Strongly Agree" GroupName="Gr1" />

In code behind, on button click I will have something like this. It means I want first to check if user has selected at least one radio buttons from each group before I submit something with SQL Command. Every CMD has different query

if (RadioButton1.Checked)
{
    SqlCommand cmd = new SqlCommand("My query here", con);
    cmd.ExecuteNonQuery();
}

if (RadioButton2.Checked)
{
    SqlCommand cmd = new SqlCommand("My query here", con);
    cmd.ExecuteNonQuery();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
skofi
  • 65
  • 10
  • This is what I have. It works fine if I select every radio button. I just don't know if there is a way to check if radio button is selected, I need to force user select one from each group in order to submit commands. – skofi Mar 20 '19 at 19:33
  • Then you should iterate your radio buttons, and check the GroupName property, if one is selected from each GroupName, they can submit else throw out an error. If each GroupName is unique, you could create a Dictionary and set the keys as each GroupName and set the boolean value to true if one radio button with that GroupName is checked, else leave it as false, then after iteration check your dictionary to see if you have any values of False. – Ryan Wilson Mar 20 '19 at 19:35
  • Each GroupName is unique, any code example how to do that? – skofi Mar 20 '19 at 19:36
  • https://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type – Ryan Wilson Mar 20 '19 at 19:38
  • No sure how to implement that in my case, :( – skofi Mar 20 '19 at 19:45
  • You know how to get your window object (which contains your radio buttons)? – Ryan Wilson Mar 20 '19 at 19:51
  • No I don't know – skofi Mar 20 '19 at 19:58
  • Does your window have a name property? – Ryan Wilson Mar 20 '19 at 19:58
  • No it doesn't have – skofi Mar 20 '19 at 20:43

1 Answers1

3

The following method will give you back the RadioButton controls that are immediate children of a parent Control:

private IEnumerable<RadioButton> GetRadioButtons(Control container, string groupName)
{
    return container.Controls
        .OfType<RadioButton>()
        .Where(i => i.GroupName == groupName);
}

For instance, if the radio buttons with the group name "Gr1" are immediate children of your form, you can get them like this:

var radioButtons = GetRadioButtons(Form, "Gr1");

Checking if any of them are checked could be done like this:

var radioButtonCheckedInGr1 = GetRadioButtons(Form, "Gr1").Any(i => i.Checked);

Hope this helps.

yv989c
  • 1,453
  • 1
  • 14
  • 20
  • 1
    Thank you a lot. This did the job. It solved my problem. It was the answer I was looking for. Thanks. Cheers – skofi Mar 21 '19 at 07:58