asp.net validation
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, 开发者_Python百科a textfield must have some input too.
How can I get this done?
Thanks in advance@!
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)
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");
}
精彩评论