Do validations still fire in ASP.NET even if the controls are hidden?

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 page? 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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

The control validation does not fire if what it is trying to validate is invisible. visible=”false”

It will however still validate it you instead do hidden=”true” on that control so that it doesn’t display to the user but want it to validate it.

Method 4

Yes, the validators will fire if they are hidden on client-side.

If a validator and it’s control are hidden on client-side by using display=”none” or visibility=”hidden” CSS attribute, then the validator will still fire both on client-side and server-side.

If you want validators to not evaluate/fire on client-side and server-side, you must set either of these properties on the validators in code-behind: Visible="false" or Enabled="false"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x