Forum Moderators: open
For layout reasons I couldn't use a list so I had to group them with GroupName.
<asp:RadioButton Text="1" ID="RadioButton1" GroupName="myGroup" runat="server" />
<asp:RadioButton Text="2" ID="RadioButton2" GroupName="myGroup" runat="server" />
<asp:RadioButton Text="3" ID="RadioButton3" GroupName="myGroup" runat="server" />
<asp:RadioButton Text="4" ID="RadioButton4" GroupName="myGroup" runat="server" />
<asp:RadioButton Text="5" ID="RadioButton5" GroupName="myGroup" runat="server" />
<asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" />
protected void cvRadioButtonGroup_ServerValidate(object source, ServerValidateEventArgs args)
{
bool itemSelected = false;
// get all radio buttons on the page
foreach (Control c in form1.Controls)
{
if (c is RadioButton)
{
RadioButton rb = (RadioButton)c;
if (rb.GroupName == "myGroup" && rb.Checked == true)
{
itemSelected = true;
}
}
}
args.IsValid = itemSelected;
}