.NET Framework - Validators and JavaScript

Asked By Nathan Sokalski on 27-Sep-08 05:36 PM
When writing a validator by inheriting from the BaseValidator class, you
need to include a JavaScript function that takes one parameter (the span tag
generated by the validator) and returns true or false. This is great for
validators that can do validation client-side, but I am writing a validator
that requires server-side interaction with a database (it validates that a
value is unique). I would like to add the ability to have this validator use
ajax/client callbacks to display the validation result without a full
postback. I have used ajax and client callbacks many times in the past, but
my problem here is getting the result returned from the server to be applied
the same way the result from the JavaScript function assigned to the
evaluationfunction attribute of the generated span tag would be. Can anybody
help me here? Thanks.
Nathan Sokalski

bruce barker replied on 27-Sep-08 07:09 PM
this will not work without a little hacking. ajax is async (as name
imples) while validation is sync (called during the postback event).

your validator could return false (to stop the postback), start a ajax
call. when the ajax call returns, if all is ok, it could restart the
postback (which will call your validator again which should return true
this time). you could use ajax on the onblur event to call the server.
hopefully most of the time the server will return a result before the
use starts a postback.

also remember ajax will be a lot slower on real server talking to an
internet client then it is on you dev box (may take seconds to return).

-- bruce (sqlwork.com)