Need help programming a custom validator

R

Rich Squid

Hello

Here's my basic problem:

On my asp.net form page I have a DetailsView (default mode=edit) bound
to a AccessDataSource control. Users can successfuly update a databound
template field, but I wanted to add a custom validator to check that
the account number they are entering exists in a table in the Access
Database.

I've tried adding another AccessDataSouce (which lists all the accounts
numbers in the database) and attempted to programmatically check if the
entered account exists; but as I've only just moved from classic ASP to
ASP.NET I get the feeling I'm coming at this from the wrong angle.

Basically what I need help with is writing the VB code for the custom
validator. Any help is greatly appreciated.

Rich
 
T

Tim Mackey

hi rich,
you need to add an event handler to the detailsview for whichever actions
you want to validate on, e.g. update / insert etc.

take a look at the events listed for this class on msdn2:
http://msdn2.microsoft.com/en-gb/library/keezbt7k.aspx
then look at ItemInserting as an example. this event is raised before the
insert actually takes place, so you can put your code in here, check if it's
all good, and if there are any problems you can set e.Cancel = true and the
insert will not go through. you can display an error message etc.

hope this does it for you. you'll find the SDK class library reference is
your best friend when you are new to .net... and possibly 5 years later if
you're still learning .net like me :)

tim
 
R

Rich Squid

Hi Tim

Thanks for the heads up.

My problem is not knowing where to put the code, but how to write the
code. I've tried putting the validation in the "ItemInserting" event of
the DetailsView (as you suggested); in the "Updating" event of the
AccessDataSource which the DetailsView is bound to; and finally (as I
mentioned in my initial post) in the code of a CustomValidator.

The code I am trying to write needs to check to see if the account
number the user is attempting to enter is valid. This can be worked out
by seeing if is value exists in another 'accounts' table which lives in
the Access Database. If the account is not found then I need to halt
the update (as you suggested) and let them know that the account is
invalid - which is why I was using a custom validator.

I'll bookmark the SDK class library as I'm sure I'll be visiting it
again in the future!! Thanks.
For me asp.net seems to be one of those "ahhh - that's how you do it"
experiences.

Thanks again,

Rich
 
T

Tim Mackey

hi rich. glad to be of help.
the customValidator sounds like the best approach.
i could be wrong on this, but i think you may still need code in the
ItemInserting event, along the lines of:

if not Page.IsValid then
e.Cancel = true
end if

the OnServerValidate event of your CustomValidator should fire before the
ItemInserting event.
if it isn't working out for you, post what you have, aspx + codebehind, and
we can take a look at it.
thanks
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top