0

How can i make a control required to put input depending on the radiobuttonlist selection?

Let me try to clarify a bit more. I got 2 radiobuttons, if one gets selected, nothing else has to be done.

But if the otherone gets selected, a textfield must have some input too.

How can I get this done?

Thanks in advance@!

2
  • What have you done so far? Where are you having trouble? Commented Dec 24, 2010 at 10:27
  • I have a 'requiredvalidation' control in my markup file. But that makes the textbox a required field, no matter what. And that should not be the case. Commented Dec 24, 2010 at 10:28

2 Answers 2

1

It all depends how you're doing your validation normally. It sounds like you're using web forms however the following methodologies will still be applicable.

There will be at least 2 ways you can do what you want. If you're validating through the use of attributes, you can create a custom validation attribute which inherits from ValidationAttribute.

More info on custom validation here

Secondly, many of the validation frameworks around today allow for some kind of ValidateSelf method within your model. This allows you to do custom validation right in your model.

Using this method you could simply do a check for the radion button value you've indicated and then decide on the appropriate action. (add error message to collection, invalidate model etc etc).

Another aproach you may find of use is to use a CustomValidator. More info on that can be found here (this is more in line with your webforms approach)

Sign up to request clarification or add additional context in comments.

1 Comment

if it is webforms being used i would just use a custom validator on the radio button list with the text box as detailed above. The first two methods seems more applicable to MVC. Depends what framework you're using.
0

You can do the validation in your code-behind:

if(radioButton1.Checked && string.isNullOrEmpty(txtTextBox.Text))
{
   MessageBox.show("You need to enter data in the text field");
}

2 Comments

Isn't MessageBox.show for win forms and this has been flagged as asp.net??
Ok...then we can write that error message to the web form, or do anything with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.