TheGateKeeper
TheGateKeeper

Reputation: 4540

Asp.net validation controls and server side validation

I recently found out that you need to perform some checks in the server side code to check if the page is valid or not. I previously was under the assumption that execution automatically stops whens a validator finds incorrect input.

I put the following code on my events that require validation:

    if (!Page.IsValid)
        return;

Is this the correct way to do it?

Also, if my page has two validation groups will the correct group automatically run on the server (the one that was triggered by the button) or do I need to call it using Page.Validate("groupName")?

Thanks

Upvotes: 0

Views: 5092

Answers (1)

Jeremy
Jeremy

Reputation: 9030

It's best to check on both client and server. The client part should be done automatically for you, but it's a defensive measure to check it on the server in case someone has bypassed your UI using whatever means.

You can assign a ValidationGroup to your button, which should be the same value as your ValidationGroup that you've assigned to your validators. When your button is clicked, it may perform client-side validation on the same group and, as you mentioned, will halt execution (prevent a postback).

On the server side, you will do exactly as you mentioned:

Page.Validate("WhateverGroup");
if (!Page.IsValid)
   return; //Didn't pass validation
else
   //Do whatever

If you have multiple groups, then you should check each of them if applicable (some groups may not apply depending on certain conditions which is why you would generally use groups).

EDIT

In response to your question:

The 'default' group that is checked is determined by the control that is posting the page. That is to say that if the control posting the page has "WhateverGroup" as its validation group, then only validators with "WhateverGroup" will be validated.

Page.IsValid should only be checked after you've called the Page.Validate method. Page.IsValid is just a flag that essentially gets tripped at anytime you call Page.Validate and something doesn't validate, whether you called it with or without a group.

References:

Upvotes: 2

Related Questions