Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have multiple ValidationSummary to validate my radio buttons. How can I use just 1 button to call my Validation?

I can NOT use RadioButtonList on my site. But I need to validate my radio buttons. I also can NOT have 1 of my radio buttons checked by default.

So my dilemma is to figure out how to validate my groups of radio buttons with only 1 button. I'm using ASP.NET and C#

Thanks in advance

What I have tried:

<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="sample1" DisplayMode="List" runat="server" ForeColor="Red" Font-Bold="false" />
<asp:RadioButton ID="RadioBtn1" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="One" />
<asp:RadioButton ID="RadioBtn2" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="Two" />

<asp:ValidationSummary ID="ValidationSummary2" ValidationGroup="sample2" DisplayMode="List" runat="server" ForeColor="Red" Font-Bold="false" />
<asp:RadioButton ID="RadioBtn3" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Three" />
<asp:RadioButton ID="RadioBtn4" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Four" />



<asp:CustomValidator ID="CustomValidator1" runat="server" ValidationGroup="sample1"
ErrorMessage="Please select one of the radio button"
OnServerValidate="ValidateSample" />

<asp:CustomValidator ID="CustomValidator2" runat="server" ValidationGroup="sample2"
ErrorMessage="Please select one of the radio button"
OnServerValidate="ValidateSample" />

<asp:Button ID="Button1" runat="server" ValidationGroup="sample1" Text="Submit" />
<asp:Button ID="Button2" runat="server" ValidationGroup="sample2" Text="Submit" />



protected void ValidateSample(object source, ServerValidateEventArgs args)
{
if ((RadioBtn1.Checked) || (RadioBtn2.Checked) || (RadioBtn3.Checked) || (RadioBtn4.Checked))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Posted
Updated 15-Feb-18 8:45am
Comments
BillWoodruff 14-Feb-18 0:14am    
Unclear: RadioButtons (grouped) allow only one selected/checked item: why do you need to validate ?

1 solution

Validation groups are intended to provide different sets of validation for different buttons. If you want all of the validators to fire when you click one button, then either don't set the validation groups, or set them all to the same value.

The validation summary is used to display a summary of all validation error on the form. It is not meant to display the validation errors for a single control. Use the validator control to do that.
ASP.NET
<asp:CustomValidator runat="server" ForeColor="Red" Font-Bold="false" Display="Dynamic" Text="Please select one of the radio buttons" OnServerValidate="ValidateSample1" />
<asp:RadioButton ID="RadioBtn1" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="One" />
<asp:RadioButton ID="RadioBtn2" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="Two" />

<asp:CustomValidator runat="server" ForeColor="Red" Font-Bold="false" Display="Dynamic" Text="Please select one of the radio buttons" OnServerValidate="ValidateSample2" />
<asp:RadioButton ID="RadioBtn3" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Three" />
<asp:RadioButton ID="RadioBtn4" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Four" />
C#
protected void ValidateSample1(object sender, ServerValidateEventArgs e)
{
    args.IsValid = RadioBtn1.Checked || RadioBtn2.Checked;
}

protected void ValidateSample2(object sender, ServerValidateEventArgs e)
{
    args.IsValid = RadioBtn3.Checked || RadioBtn4.Checked;
}

Now, do yourself a favour and give your controls meaningful IDs, rather than accepting the defaults provided by Visual Studio. You might remember what RadioBtn42 means now, but when you come back to your code in six months time, you'll be cursing the person who wrote it!
 
Share this answer
 

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