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:
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:
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;
args.IsValid = false;
break;
}