I’m racking my brain trying to figure this out…
I have an ASP.NET web form with some dynamically generated form fields. Depending on what value is chosen in a dropdown list, visibility is set on the following textbox. I do this with a client-side script.
The intent is a user selects “Other” from a dropdownlist, and the “Other Description” field appears next on the form. I’d like to add the ability to activate a requiredfield validator or some kind of validation on the “Other Description” textbox when the textbox is visible.
Any thoughts on how to accomplish this? In my testing postbacks weren’t an option, since the form field needs to be on the page so an empty value is associated with it. (Not my choice – I inherited this code from another developer)
The most common approach would be to always render the validator, but render it as disabled and then enable it when desired.
Take a look at the documentation on ASP.NET validators, paying particular attention to the section labelled “Client-Side Validation” and, more specifically, the
ValidatorEnable(val, enable)function.Note that the
valparameter is the validator element, not a string ID.