Server-Side Validation

A couple of weeks ago, I was talking to someone who is doing a project for us. He is fairly new to .NET programming, so when I told him that I wanted both server-side and client-side validation, it scared him a little bit.

He knew how to do client-side validation using the built-in validation controls in ASP.NET. This is fairly easy to do. You put the validation control into your aspx file, and connect it to the control you want to validate. Then ASP.NET will include the required JavaScript for the client to do validation. The problem is that this JavaScript doesn’t run if the client has JavaScript turned off, or if the client doesn’t support JavaScript.

He thought he’d have to write the code to do the server-side validation. He wasn’t aware that you could use .NET’s validation controls on the server-side as well as client-side. All you need to do is add the following code to the beginning of your button handler:

if (!Page.IsValid)

This cause the server-side to run through the validation controls. If any fail, then you return to web page, which will now include the error messages from your validation controls. If all the validation controls pass, then it will go on to the next line in the button handler.

It really is this easy to include server-side validation in ASP.NET!

%d bloggers like this: