Mark W
Mark W

Reputation: 811

Radio Button validation event

I have a simple form with one text box and a panel with three radio buttons in it. I have used a validation event with an error provider to force the user to place a number in the text box. My problem is with the group of radio buttons. The user needs to select a radio button. I found that you can't validate on the panel containing the radio buttons, instead I had to write a validation event for each button. Is there a simple way to make sure that the user has selected a radio button? Thank you.

Upvotes: 1

Views: 3354

Answers (3)

Jay Riggs
Jay Riggs

Reputation: 53593

A simple approach is to simply use this code:

bool selectionMade = radioButton1.Checked || radioButton2.Checked || radioButton3.Checked;

You don't need to put this code in any validation event, you can put it wherever you need to make sure a RadioButton is checked. This can be in the click event for a button that saves the current record, etc. If selectionMode is false, trigger whatever user notifications you need to.

If you really need to use one of the RadioButton's validating event, you could create just one such event and wire all your RadioButtons to use that one event. You can the the event's object sender argument to find out which RadioButton trigger the validating event.

Upvotes: 1

Brett Veenstra
Brett Veenstra

Reputation: 48474

I would use a GroupBox, put your RadioButton controls inside it and use the Validating event on your GroupBox.

Upvotes: 1

Mongus Pong
Mongus Pong

Reputation: 11477

You could use a Custom Validator

Specify a ClientValidationFunction with code along the lines of

<script language="javascript">
   function ClientValidate(source, arguments)
   {
        if ($('#button1').checked || $('#button2').checked || $('#button3').checked ) {
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

Upvotes: 0

Related Questions