Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have multiple radio buttons and I'm using a group name to choose at least 1 of the 2 options. I can't seem to get the GroupName so I can validate them with a submit button is clicked.

Thanks for any help

What I have tried:

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="* Select an option" ForeColor="#ff0000" OnServerValidate="option1_Validation" Display="Dynamic" /> 

<myRepeater>

<asp:RadioButton ID="rdOption1" Text="Option_1" GroupName="gnOption1" runat="server" />

<asp:RadioButton ID="rdOption2" Text="Option_2" GroupName="gnOption1" runat="server" />

</myRepeater>

 protected void option1_Validation(object source, ServerValidateEventArgs args)
    {
        bool itemSelected = false;
        foreach (RepeaterItem ri in myRepeater.Items)
        {
            RadioButton rb= (RadioButton)ri.FindControl("gnOption1");
            {               
              if (rb.GroupName == "gnOption1" && rb.Checked == true)
                {
                    itemSelected = true;

                }
                args.IsValid = itemSelected;
            }
        }
    }
Posted
Updated 22-May-18 9:33am
v2
Comments
Richard Deeming 22-May-18 15:33pm    
ri.FindControl("gnOption1")


No, your control's ID is not "gnOption1" either.

1 solution

Quote:
RadioButton rb= (RadioButton)ri.FindControl("Game_1");

The FindControl method[^] expects a parameter representing the ID of the control you want to find.

You do not have a control with the ID Game_1 inside your repeater, so FindControl will return null, and you will get a NullReferenceException.

You need to pass in the correct ID for the control you're trying to find:
C#
bool itemSelected = false;
foreach (RepeaterItem ri in myRepeater.Items)
{
    RadioButton rb = (RadioButton)ri.FindControl("rdOption1");
    if (rb.Checked) 
    {
        itemSelected = true;
        break;
    }
}

args.IsValid = itemSelected;

NB: This will validate whether "Option_1" is selected in any item. If that's not what you're trying to validate, then you'll need to explain your requirement.

EDIT: If you want to validate that one of the radiobuttons is selected in each item:
C#
args.IsValid = true;

foreach (RepeaterItem ri in myRepeater.Items)
{
    RadioButton rb = (RadioButton)ri.FindControl("rdOption1");
    if (rb.Checked) continue;
    
    rb = (RadioButton)ri.FindControl("rdOption2");
    if (rb.Checked) continue;
    
    // Neither option is selected:
    args.IsValid = false;
    break;
}
 
Share this answer
 
v2
Comments
Commish13 22-May-18 15:33pm    
Sorry but the ("Game_1") should have been ("gnOption1"). I'm trying to validate to that 1 of the 2 radio buttons must be selected.
Richard Deeming 22-May-18 15:36pm    
As I said, the FindControl parameter needs to be the ID of your control.

You can't pass the GroupName of your control and expect it to find anything.

The IDs of your radiobuttons are rdOption1 and rdOption2. Those are the strings you can pass to FindControl.
Commish13 22-May-18 15:51pm    
Ok. I understand what your saying. This will validate if the radio button rbOption1 is selected. But, I'm trying to validate if 1 of the 2 radio buttons are selected than it would be valid. That's why I was trying to use the GroupName. Is there a way I could validate these 2 radio buttons at the same time to ensure 1 of the 2 options are selected?
Commish13 22-May-18 20:22pm    
Thank you your second code works great!
Vincent Maverick Durano 23-May-18 0:21am    
5ed!

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