views:

983

answers:

4

I have an application that uses RequiredFieldValidator controls with a ValidationSummary and once a "Finish" button is clicked all the fields are validated. This page has several different parts of it that are made visible or invisible and I noticed that the Validators only activate for visible controls and ignore the invisible ones. This is a useful functionality that is utilized by my program.

however....

I have a need to validate controls that aren't visible since I am splitting up parts of one long page into segments. So I was wondering if there is a way to manually force visible = false validation controls (or ones that are part of an invisible table, etc) to be validated on Page.IsValid, without having to make them visible again?

Thanks in Advance!

P.S. I tried using the ValidationGroup property to see if that would catch them but it didn't.

A: 

if the page is divided into segments and only part of the page is shown, couldn't you validate on the method that shows the new segment? The valid status of the control shouldn't be affected once it has been made invisible. (Unless there is a dependancy i guess.).

Don't let them switch segments of the page with invalid input.

an alternative would be to use css to make the controls invisible rather than server side. The controls could be in a div with display: none; All the controls would be rendered along with the validation summary.

Jeff Martin
A: 

If you're making these controls invisible server-side, then they're not even rendered on the client to be validated.

Are you using AJAX to switch between your sections/make the controls invisible? If so then you should probably validate the controls on those partial page postbacks (as Jeff Martin suggested) before toggling the controls invisible.

I think we need some more info on how you're doing this to give you a good answer.


Alright so no AJAX (based on your comment)... if you're using ASP.NET Validation controls they have a client-side portion and a server-side portion. You can see if the validators are valid in your event handlers in your code-behind. You may have to do something specific to get them to fire their validation code server-side, and your invisible controls may not retain their values between multiple postbacks. But it should be doable.

The gist here is that ASP.NET Validation controls can be set so they don't do anything client-side, but they still provide validation server-side. You're essentially in that boat.

I think this MSDN article has all you need.

sliderhouserules
No AJAX is used, the page posts back when "Finish" is clicked. What's being used to switch between the mini-pages is an asp.net LinkButton that cycles the Visible controls based on what page they are on (total of 7 pages). That button also posts back, the code is server-side (in the codebehind page)
n2009
+1  A: 

If the control isn't .Visible then the html isn't there to validate against. Instead of setting Control.Visible = false, try setting a css class to hide it instead.

In your style sheet:

.invisible { visibility:hidden; }

And then instead of .Visible = false use:

Control.CssClass = "invisible";
Mike Comstock
good idea, I like it! Simple and flexible. :) I will give it a try.
n2009
I had class HtmlTable for these, I changed it to WebControls.Table and got it working with CssClass but it leaves empty space where the element was. I need to completely hide the table from rendering in a blank space because of page spacing considerations.
n2009
try display:none instead of visibility:hidden
Mike Comstock
A: 

In response to the 2nd comment to Mike Comstock's post (I don't have enough reputation to comment yet :( )...

to remove the spacing try:

.invisible { visibility:hidden; display:none; }
Trev