471,306 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Server Side Validation

Sanity Check Requested!
I've been writing code way too long for the past several days and just came
across this bit of logic I wrote several weeks ago. Suddenly something seems
wrong with it and I'm not sure what. I just would appreciate someone to look
over my shoulder (so to speek) and let me know if you think it's okay as is.

The context (of the code snippet below) is this: I dynamically add controls
and validators for those controls to a form at runtime. During PostBack the
following code executes - with the intent to prevent further server-side
processing if any of the controls do not pass validation (in cases where
client-side validation somehow was bypassed). Here's the snippet - my
paranoia is described below the snippet:

if ( ! Page.IsValid) {
if (Page.Validators.Count > 0) {
string strTemp = "";
foreach(System.Web.UI.IValidator currentValidator in Page.Validators) {
if ( ! currentValidator.IsValid) {
strTemp += currentValidator.ErrorMessage + "<br>";
}
}
if (strTemp.Length > 0) {
// Present the validator messages to the user AND prevent further
server-side processing
// ...
}
}
}

That's it. My paranoia is that it looks *possibly* wrong to test for
Page.IsValid first. Shouldn't I first loop through the Page.Validators
collection and trigger validation on each control? Speaking of which - does
[if ( ! currentValidator.IsValid) {] trigger the validation, or simply tell
me the result of any prior attempt to validate the data? If so, then how
would I actually trigger the validation - server side? Maybe not - maybe
everything is fine as is. See? I'm paraniod. (of course "just because you're
paranoid doesn't mean you're not being followed!" ...thus my post here).
This code seemed to make perfect sense a couple of weeks ago.

Thanks!
Nov 19 '05 #1
1 1670
Frankie,

Page validation gets triggered automatically on a postback. I have noticed
that if you are using a control's auto post back function (E.g. an auto post
back on a checkbox) that page validation isn't fired for that.

If a button click is trigerring your post back don't bother to call page
validation on your own. If it's an auto post back then fire page validation
manually with Page.Validate then you only need to check if the whole page is
valid or not, there is no need to check each validator yourself, that's what
Page.IsValid does.

So:

Page.Validate //only need to call this method if routine is running because
of a control's auto post back a button click would have already run
Page.Validate

if (Page.IsValid) {
//run your code here
}

If the page isn't valid the error message you've set on the validators when
they were created will be displayed automatically.

You've really recreated a lot of functionality that happens automatically...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Frankie" <Ta******@BeanTown.org> wrote in message
news:OO****************@TK2MSFTNGP14.phx.gbl...
Sanity Check Requested!
I've been writing code way too long for the past several days and just
came across this bit of logic I wrote several weeks ago. Suddenly
something seems wrong with it and I'm not sure what. I just would
appreciate someone to look over my shoulder (so to speek) and let me know
if you think it's okay as is.

The context (of the code snippet below) is this: I dynamically add
controls and validators for those controls to a form at runtime. During
PostBack the following code executes - with the intent to prevent further
server-side processing if any of the controls do not pass validation (in
cases where client-side validation somehow was bypassed). Here's the
snippet - my paranoia is described below the snippet:

if ( ! Page.IsValid) {
if (Page.Validators.Count > 0) {
string strTemp = "";
foreach(System.Web.UI.IValidator currentValidator in Page.Validators) {
if ( ! currentValidator.IsValid) {
strTemp += currentValidator.ErrorMessage + "<br>";
}
}
if (strTemp.Length > 0) {
// Present the validator messages to the user AND prevent further
server-side processing
// ...
}
}
}

That's it. My paranoia is that it looks *possibly* wrong to test for
Page.IsValid first. Shouldn't I first loop through the Page.Validators
collection and trigger validation on each control? Speaking of which -
does [if ( ! currentValidator.IsValid) {] trigger the validation, or
simply tell me the result of any prior attempt to validate the data? If
so, then how would I actually trigger the validation - server side? Maybe
not - maybe everything is fine as is. See? I'm paraniod. (of course "just
because you're paranoid doesn't mean you're not being followed!" ...thus
my post here). This code seemed to make perfect sense a couple of weeks
ago.

Thanks!

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Hernán Castelo | last post: by
1 post views Thread by John Chan | last post: by
5 posts views Thread by =?Utf-8?B?dXNmaW5lY2F0cw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.