473,394 Members | 1,285 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1749
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hernán Castelo | last post by:
should i to validate all the "Request"s calls like Request.FORM("...") and Request.Cookies("...") ???? if it is so, i have to see inside every "Input" elements like "Text" and even "Hidden"...
4
by: earwicker | last post by:
I recently deployed a web application which contains a user registration form with the usual fields: name, address, email, password, etc. Each of the TextBoxes uses a validation control to verify...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
4
by: usl2222 | last post by:
Hi folks, I appreciate any assistance in the following problem: I have a form with a bunch of dynamic controls on it. All the controls are dynamically generated on a server, including all...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
2
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
by: John Chan | last post by:
Hi, Im doing a maintenance application in ajax and coldfusion at work on IE6 exclusively. I have a save button on each form and i have to do various validations server side and on client side...
5
by: =?Utf-8?B?dXNmaW5lY2F0cw==?= | last post by:
I'm new to asp.net. I'm trying to follow the tutorial walkthroughs and have got a few done. Now I'm doing the one about validating input, and I've run into some problems. In particular, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.