Friday, January 30, 2009

.NET Validation Controls: a quick look again.

You can call this my ignorance or something else. After working in .NET for over 3 years, today I cleared a doubt that


  1. If we are using the .Net Validation controls then do I need to code for the server side also.
  2. What happens if the browser JavaScript is disabled?

These were some of the question of which I did not know the answer. After one of my friends (Kumar) asked me, I gave it a shot.


I am writing the conclusion of my experiment below.


  1. The .NET validation controls are good for both Client side validation and as well as for the Server side validations. It means that no special code is required to do validation on the server side.
  2. If the EnableClientScript is False, then the validations occurs Server side, automatically. (No need for any code.)
  3. If the JavaScript is disabled in the browser, then the validation controls will behave as if the EnableClientScript is False i.e. the Server side validations are done, automatically again no need for any code.

So, if the EnableClientScript is False or the JavaScript is disable in the browser the server side validation


Now, the important thing to remember is that in the code-behind, we should always sandwich the code between if (Page.IsValid) i.e.


if (Page.IsValid)

{

//Code here, e.g. Call the Data Access Layer Method here.

}


With the above code, if the Server side validation occours and if it fails then the code will NOT execute. But if we do not put the Page.IsValid thing, then it will should the Validation Error Message and it will execute the code as well!!


Please let me know, if I am missing something or if you want me to add something.


No comments: