views:

142

answers:

2

Here's a curious case: I have a form with several data-input controls on it. A sub-set of these controls can be shown / hidden by checking a checkbox. All controls and their associcated Validators belong to a ValidationGroup called "Advanced", as does the asp:Button Submit button and a ValidationSummary.

The thing is, I don't want the sub-set of fields to be validated if they are hidden; they are only required if they are visible.

A really unpleasant way of doing it would be to have another asp:button that gets shown / hidden along with the extra fields, and the normal submit button gets show / hidden, but add in another couple of sub-sets of fields and it gets a bit messy...

Any thoughts? Would dearly love to stick with my tried and trusted Validation controls...

Thank you,

Mike K.

+1  A: 

Can you set it up so that when you set the fields to "hidden" you also Disable the validation?

protected void chkBoxChecked(object sender, Eventargs e) {
     validationControl.Enabled = false;
}

Of course you would need to make sure that our checkbox had it's AutoPostBack property set to True;

If you want to avoid using Postback/Flash, of course you could probably wrap the whole thing in an UpdatePanel, or you could do it all in javascript:

Using Javascript to disable validators

Jack Marchetti
It's all done with JavaScript (as opposed to smoke and mirrors), as an attempt to minimise PostBack.
Mike Kingscott
try it in javascript then: http://geekswithblogs.net/jonasb/archive/2006/08/11/87708.aspx
Jack Marchetti
I certainly will Jack :-)
Mike Kingscott
And it worked a treat :-)
Mike Kingscott
A: 

See this question. You should disable the validators when the controls they validate are hidden. The two answers show how to disable the validators on the client side and on the server side.

Jason Berkan