views:

102

answers:

2

I have a form that uses ASP.NET validations. I am using some inline C# in the aspx to show/hide certain controls depending on a user's role. I would use the Visible property, but there are so many of them, I just decided to do inline C# to show and hide (I know, not best practice, but bear with me for a second). I am having an issue where Page.IsValid is always set to False when I submit my form (when certain fields are being hidden). Will the validations still fire off even if the controls are not even rendered on the pag? Also, if this is not the case, is there an effective way of breaking down Page.IsValid to find out what is setting it to False? Thanks.

A: 

The validators would still fire, you need to hide them as well

alejandrobog
so you are saying that I need to set the Visible property to false on all of them?
Josh
yes, if you are on a databound context you can bind the visibility to a property. <asp:RequiredFieldValidator Visible='<%# ShowValidatos>'
alejandrobog
Okay thanks. The only thing is that I would guess if the validations are getting fired behind the scenes, that the required field validators would prevent the form from even being submitted at all (which isn't the case on my end). Any ideas?
Josh
Yes its weird i just make testpage with a requiredfieldvalidator and an invisible textbox and the page gets submitted and after the postback the ErrorMessage appears
alejandrobog
+3  A: 

If you set Visible to false, validation for that control will not fire. From ASP.Net Validation in Depth:

Why not just use Visible=false to have an invisible validator? In ASP.NET the Visible property of a control has a very strong meaning: a control with Visible=false will not be processed at all for pre-rendering or rendering. As a result of this stronger meaning, Visible=false for a validator means that not only does not it not display anything, it is does not function either. It is not evaluated, does not affect page validity, and does not put errors in the summary.

If you want a control to validate but have it hidden on the page, use CSS to set display to none.

womp
Thanks for the response. This totally makes sense. Do I need to set the Visible property on the validation and the actual control itself? Or just the validation? I am a little flustered with this since my page is doing a PostBack and I have about 10 required field validators (hidden) that are not getting fired off. If you just hide the HTML using CSS, does it only fire off the custom/server-side validations? Please advise.
Josh